Recent

Author Topic: Error 203 (heap error) on image.picture.loadfromfile  (Read 3326 times)

straetch

  • Jr. Member
  • **
  • Posts: 75
Error 203 (heap error) on image.picture.loadfromfile
« on: May 28, 2016, 01:12:47 pm »
I declared a TImage object in the IDE.
The program displays several images on that object using multiple image.picture.loadfromfile .
The first image display is as expected. However, the program hangs on a following display, sometimes on nr. 3, or on nr. 4. The program freezes for about 30 seconds, then shows a heap error (error 203) when in IDE mode. It shows an Out of Memory when run directly from the .exe .
The directory is small (some 30 files).
What can I do?

Below the simplified code. The filenames in a directory are copied to a stringlist; then the image files are displayed one by one.

MyFileList.Clear;
if FindFirst (MyDir + '\' + '*.*', faAnyFile, sr) = 0 then begin
  MyFileList.Add(sr.Name);
  while FindNext(sr) = 0 do
      MyFileList.Add(sr.Name);
  FindClose(sr);
  for i := 0 to 11 do begin
    try
       MyImage.Picture.LoadFromFile(MyDir + '\' + MyFileList);
    except
      on E: Exception do
          showmessage('This is not an image file');
    end;
    showmessage('Continue with the next one');
  end;
end;

Using Lazarus 1.4.4 with FPC 2.6.4.     Windows7.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #1 on: May 28, 2016, 01:29:51 pm »
Uncompilable code snippets that have been changed from the code that actually gives rise to the problem are not very useful.

However, assuming your code is really more like this:
Code: Pascal  [Select][+][-]
  1.   for i := 0 to 11 do begin
  2.     try
  3.       MyImage.Picture.LoadFromFile(MyDir + '\' + MyFileList[i]);
  4.     ...

you appear to be trying to load 12 different image files successively into the same image, which is not going to do anything good, and is almost certainly not what you intended to accomplish.
What are you trying to achieve?

straetch

  • Jr. Member
  • **
  • Posts: 75
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #2 on: May 28, 2016, 01:52:51 pm »
I tried to minimise my program to the part that is useful for diagnostics.
You are right, the index in MyFileList was missing. Sorry.
The purpose of the program is to display images one by one, where the user should fill in comments for each picture in a TMemo. All comments are then saved together in a text file.
Thanks.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #3 on: May 28, 2016, 02:21:38 pm »
Perhaps that part of your program in pseudocode could be organised something like this:
Code: Pascal  [Select][+][-]
  1.   for i:=0 to MaxFileImageIndex do
  2.     begin
  3.       LoadImage(i);
  4.       GetUserCommentFor(i);
  5.     end;

You can then work out how to implement the LoadImage() and GetUserCommentFor() procedures.

straetch

  • Jr. Member
  • **
  • Posts: 75
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #4 on: May 28, 2016, 08:15:41 pm »
Thanks for your response.
Your pseudocode is indeed in essence the central loop of the program.
Nevertheless, also here we execute consecutive MyImage.Picture.LoadFromFile( ) calls that generate the same issue as mentioned: the LoadImage(i) procedure is executed in the loop and does contain the LoadFromFile.
It looks like TImage.Picture does not support consecutive LoadFromFile calls. I would assume LoadFromFile would first purge an already existing image and deallocate memory before loading a new image.
There seems to be no function to let me do the purging programmatically before calling the LoadFromFile (other than completely creating and destroying the TImage at every LoadImage).

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #5 on: May 28, 2016, 08:47:44 pm »
Can you post details about these pictures, including extension, width, height, bits?
Your PC specifications?

TImage is not the fastest control. LoadFromFile indirectly selects a suitable TGraphicClass descendent to load the image. There might be a bug in one of these classes, or maybe these images are huge.

straetch

  • Jr. Member
  • **
  • Posts: 75
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #6 on: May 28, 2016, 10:01:32 pm »
All images in the test are .jpg (though I intend to use .tif as well).
Widths of the images are between 2327 and 4239 pixels.
Heights between 2363 and 3483 pixels.
Largest file size 4897 kB.
All these pictures display perfectly on various image viewers and processors. Any of these pictures shows well with TImage if it is first in the row.

My PC has a Intel Core i5 750 processor with 12 GB dynamic RAM. Motherboard is MSI P55.
The machine is reasonably fast. I run Photoshop on large pictures smoothly.
OS is Windows 7 Home Premium 64-bit.
Avira scan did not detect any viruses.

Is there an alternative to TImage I could use?


wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #7 on: May 28, 2016, 10:58:23 pm »
Is there an alternative to TImage I could use?
No need for an alternative. I am absolutely sure that TImage is doing fine. I ran the following code for my photo folder with thousands of images, and no issue occured. If something goes wrong it must be in your code after loading the images.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   path = 'D:\Photos';
  4. var
  5.   l: TStringlist;
  6.   i: Integer;
  7. begin
  8.   l := TStringlist.Create;
  9.   try
  10.     FindAllFiles(L, path, '*.jpg', true);
  11.     for i:=0 to L.Count-1 do begin
  12.       Image1.Picture.LoadFromFile(L[i]);
  13.       Label1.Caption := L[i];
  14.       Application.ProcessMessages;
  15.     end;
  16.   finally
  17.     L.Free;
  18.   end;
  19. end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Error 203 (heap error) on image.picture.loadfromfile
« Reply #8 on: May 29, 2016, 01:42:56 am »
I agree with wp. Try his code.

 

TinyPortal © 2005-2018