Board index » delphi » TImage & Memory problem

TImage & Memory problem


2004-09-09 08:31:29 PM
delphi1
Hello,
I have an application that displays photographs in a folder stored as .jpg.
I use a TImage on a form, and everytime the user presses a cursor key, a new
image is loaded and displayed in the TImage. The problem is that everytime
the user selects a new image, the memory usage goes up. Eventually windows
(2000) reports that virtual memory is running low and it resizes it. I have
found (from a previous post)that I can prevent this if I minimize the
application, load the new image and then restore the application which isn't
really acceptable. Is there something I can do to force whatever is
happening during the minimize / restore without actually having to minimize?
Ian
 
 

Re:TImage & Memory problem

It sounds like you forget to free a resource. This could either be the
bitmap, the jpeg, or some other kind of stream.
Always make sure that whenever you create an object, you also free it. Make
sure to wrap in try..finally blocks to ensure that the destructor is called.
Here's an example:
uses
Jpeg, Graphics;
procedure MyForm.ReadNextImage(Filename: string);
var
AJpg: TJpegImage;
ABmp: TBitmap;
begin
AJpg := TJpegImage.Create;
ABmp := TBitmap.Create;
try
AJpg.LoadFromFile(Filename);
ABmp.Assign(AJpg);
//.. do whatever preprocessing is neccesary to the bitmap here.
// Assign it to the image on your form
Image1.Picture.Bitmap.Assign(ABmp);
finally
// Here we free the objects that we created again
ABmp.Free;
AJpg.Free;
end;
end;
Kind regards,
Nils
www.simdesign.nl
"Ian Hodgson" <XXXX@XXXXX.COM>writes
Quote
Hello,

I have an application that displays photographs in a folder stored as
.jpg.
I use a TImage on a form, and everytime the user presses a cursor key, a
new
image is loaded and displayed in the TImage. The problem is that everytime
the user selects a new image, the memory usage goes up. Eventually windows
(2000) reports that virtual memory is running low and it resizes it. I
have
found (from a previous post)that I can prevent this if I minimize the
application, load the new image and then restore the application which
isn't
really acceptable. Is there something I can do to force whatever is
happening during the minimize / restore without actually having to
minimize?

Ian


 

Re:TImage & Memory problem

Thanks Nils,
I was using the GraphicsConversionsLibrary to load the image, but loading it
directly into a TImage, each successive load must have been leaving the
previous one in memory. I have changed it to dynamically create a bitmap,
load the image into the bitmap, assign the bitmap to the image and then free
the bitmap. Seems to do the trick.
Ian
"Nils Haeck" <XXXX@XXXXX.COM>writes
Quote
It sounds like you forget to free a resource. This could either be the
bitmap, the jpeg, or some other kind of stream.

Always make sure that whenever you create an object, you also free it.
Make
sure to wrap in try..finally blocks to ensure that the destructor is
called.

Here's an example:

uses
Jpeg, Graphics;

procedure MyForm.ReadNextImage(Filename: string);
var
AJpg: TJpegImage;
ABmp: TBitmap;
begin
AJpg := TJpegImage.Create;
ABmp := TBitmap.Create;
try
AJpg.LoadFromFile(Filename);
ABmp.Assign(AJpg);
//.. do whatever preprocessing is neccesary to the bitmap here.
// Assign it to the image on your form
Image1.Picture.Bitmap.Assign(ABmp);
finally
// Here we free the objects that we created again
ABmp.Free;
AJpg.Free;
end;
end;

Kind regards,

Nils
www.simdesign.nl

"Ian Hodgson" <XXXX@XXXXX.COM>writes
news:41404d0a$XXXX@XXXXX.COM...
>Hello,
>
>I have an application that displays photographs in a folder stored as
.jpg.
>I use a TImage on a form, and everytime the user presses a cursor key, a
new
>image is loaded and displayed in the TImage. The problem is that
everytime
>the user selects a new image, the memory usage goes up. Eventually
windows
>(2000) reports that virtual memory is running low and it resizes it. I
have
>found (from a previous post)that I can prevent this if I minimize the
>application, load the new image and then restore the application which
isn't
>really acceptable. Is there something I can do to force whatever is
>happening during the minimize / restore without actually having to
minimize?
>
>Ian
>
>