* * *

Author Topic: [SOLVED] Strange bitmap bug  (Read 315 times)

MiniApp

  • Newbie
  • Posts: 3
[SOLVED] Strange bitmap bug
« on: March 05, 2017, 04:10:10 pm »
Hello,

I'm trying to create a program who need lot of pictures (I load more than 1 042 PNG's at start). I don't know why but after a massive load I have a access violation after a random time.

I think this can be a LCL bug because the bug is when I move my mouse in my main form. Stranger I have created and show a form before my massive load, and he work perfectly !

My function to load bitmaps is
Code: Pascal  [Select]
  1. function LoadPicture(const FileName:TFilename):TBitmap;
  2. begin
  3.   Result := TBitmap.Create;
  4.   LoadPicture(FileName,Result);
  5. end;
  6. procedure LoadPicture(const FileName:TFilename;const Bitmap:TBitmap);
  7. begin
  8. //  LoadPicturePicture := TPicture.Create;
  9.   InternalLogger.Info('Begin LoadPicture from "%s"',[FileName]);
  10.   try
  11.     LoadPicturePicture.LoadFromFile(FileName);
  12.     Bitmap.SetSize(LoadPicturePicture.Width,LoadPicturePicture.Height);
  13.     Bitmap.Canvas.Draw(0,0,LoadPicturePicture.Graphic);
  14.   except
  15.     on E:Exception do
  16.       begin
  17.         InternalLogger.Error('End LoadPicture fail ! [%s] %s',[E.ClassName,E.Message]);
  18.         raise;
  19.       end;
  20.   end;
  21.   InternalLogger.Info('End LoadPicture');
  22. //  LoadPicturePicture.Free;
  23. end;

I load pictures in a FormCreate and I store them into a array.

I have two times this bug and this make me  >:( . Have you a answer ?

ps:Sorry if my english is bad...
« Last Edit: March 06, 2017, 08:50:36 pm by MiniApp »
French and under the latest Ubuntu ;-p !

Thaddy

  • Hero Member
  • *****
  • Posts: 2985
Re: Strange bitmap bug
« Reply #1 on: March 05, 2017, 04:24:44 pm »
How big are these pictures?
I suspect this is caused by out-of-memory.
In that case the exception can not be handled in your code.
You can test this by making a small change to your code:
Code: Pascal  [Select]
  1.   except
  2.     // add this first. Only works if first.
  3.     on E:EOutOfMemory do
  4.        raise;  // This gets handled by a pre-allocated Dialog
  5.     on E:Exception do
  6.     begin      
  7.        // this won't work for an EOutOfMemory exception because it needs new memory
  8.        InternalLogger.Error('End LoadPicture fail ! [%s] %s',[E.ClassName,E.Message]);
  9.        raise;
  10.     end;
  11.   end;
  12.  
  13.  

I have added a screenshot.

It is usually a bad  O:-) idea to try to store 1042 png's in memory at the same time..... >:(
« Last Edit: March 05, 2017, 05:18:40 pm by Thaddy »

MiniApp

  • Newbie
  • Posts: 3
Re: Strange bitmap bug
« Reply #2 on: March 06, 2017, 06:30:26 pm »
Thank you for your reply, but it's not a memory overload. Pictures makes 128x128 (I have calculated 48MB for all pixels of pictures). And I have the same problem with 100 pictures of 64x64. I will try to use the no OS handle Bitmap.
French and under the latest Ubuntu ;-p !

MiniApp

  • Newbie
  • Posts: 3
Re: Strange bitmap bug
« Reply #3 on: March 06, 2017, 08:50:09 pm »
This work !

But look my func...
 
Code: Pascal  [Select]
  1. function LoadLCLPicture(const FileName:TFilename):TLazIntfImage;
  2. var
  3.   Bmp:TBitmap;
  4. begin
  5.   Bmp := LoadPicture(FileName);
  6.   Result := Bmp.CreateIntfImage;
  7.   Bmp.Free;
  8. end;
I don't know how is possible :o . My project is very strangly bugged (A other subproc create a undefined number of TImage, but he work only one time ! %) ).

For the  memory consumption, I'm in 50MB after start, this should not be problematic on today computer.

Note:If anyone have more information on this strange bug I listen him  O:-)
French and under the latest Ubuntu ;-p !

Thaddy

  • Hero Member
  • *****
  • Posts: 2985
Re: [SOLVED] Strange bitmap bug
« Reply #4 on: March 06, 2017, 09:25:58 pm »
Yes. CreateIntfImage makes it either an interfaced image instance or it makes a copy as an interface.
An interface is refcounted and free'd when out of scope.
It is either a copy or the refcount for bmp.free is not 0. (So it can be a fake free).
You have to find out what CreateIntfImage exactly does, though, in the sourcecode....

Nice task for you...!!

Mixing interface instantiation with class instantiation on the same class and in one program, though, is not a brilliant idea and should be avoided to keep it clean.
Strictly either / or.
« Last Edit: March 06, 2017, 09:32:42 pm by Thaddy »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus