Board index » delphi » TImage, LoadFromFile and GDI resources

TImage, LoadFromFile and GDI resources

Hi,
I use TImage with a timer to display randomly a picture between 50:

procedure TForm1.Timer1Timer(Sender: TObject);
var j: Integer;
begin
   j := Random(49);
   ImaPhoto.Picture := nil;
   try
        ImaPhoto.Picture.LoadFromFile('c:\\pictures\Pict'
+Format('%3.3d',[j])+'.bmp');
   except
        ImaPhoto.Picture := Nil;
   end;
end;

The timer interval is 500 ms.
My problem is that when i run the application, GDI resources decrease until
the application crash.
(I use Delphi 3.01 with Win 95B)

Have somone an idea to solve this problem ?
Thank you.

 

Re:TImage, LoadFromFile and GDI resources


In article <678n1o$hq...@paris.magic.fr>, "Bernard E." <mee-e...@magic.fr>
writes:

Quote
>   try
>ImaPhoto.Picture.LoadFromFile('c:\\pictures\Pict' +

Format('%3.3d',[j])+'.bmp');

Quote
>   except
>        ImaPhoto.Picture := Nil;
>   end;
>end;

>The timer interval is 500 ms.
>My problem is that when i run the application, GDI resources decrease until
>the application crash.

Use ImaPhoto.Picture.Free;

Setting the pointer to nil effectively stops Delphi freeing the memory, because
Delphi checks for a non-nil pointer before freeing the memory, so as to prevent
it GPFing on non-allocated memory.

Setting the pointer to nil _after_ freeing it is good program practice, because
Delphi does not always do it, and might attempt a re-free at some point on
memory which has already been freed.

Alan Lloyd
alangll...@aol.com

Re:TImage, LoadFromFile and GDI resources


IN the message below, if imaphoto is declared as Timage, then there is no need
to free any resources.  Isnt that correct? If it is defined as a Tbitmap (and
created) then you need to free it when finished.  Can someone verify my point
on this, as this is the way I understood it.  Thanks    

In article <678n1o$hq...@paris.magic.fr>, "Bernard E." <mee-e...@magic.fr>
writes:

Quote
>   try
>ImaPhoto.Picture.LoadFromFile('c:\\pictures\Pict' +

Format('%3.3d',[j])+'.bmp');

Quote
>   except
>        ImaPhoto.Picture := Nil;
>   end;
>end;

>The timer interval is 500 ms.
>My problem is that when i run the application, GDI resources decrease until
>the application crash.

Use ImaPhoto.Picture.Free;

Setting the pointer to nil effectively stops Delphi freeing the memory, because
Delphi checks for a non-nil pointer before freeing the memory, so as to prevent
it GPFing on non-allocated memory.

Setting the pointer to nil _after_ freeing it is good program practice, because
Delphi does not always do it, and might attempt a re-free at some point on
memory which has already been freed.

Alan Lloyd
alangll...@aol.com

Re:TImage, LoadFromFile and GDI resources


At leat, i found a solution:

All my picture whas 256 color, BMP RLE encoded. I have change for 256 color
BMP RGB encoded and now all is OK.

Other Threads