Recent

Author Topic: heaptrc - 3 time false positive  (Read 918 times)

winni

  • Hero Member
  • *****
  • Posts: 3197
heaptrc - 3 time false positive
« on: April 18, 2019, 12:01:49 am »
Hi!

I ported a project from laz 1.6 to laz 2.0. In 1.6 there where no memory leaks - if you can believe heaptrace. In 2.0 suddenly 3  not  freed blocks appeared - pointing to strange lines of code

1. Pointed to rasterimage.inc, line 277, that is
Code: Pascal  [Select][+][-]
  1. constructor TRasterImage.Create;
  2. ...
  3. -->     FSharedImage := GetSharedImageClass.Create;  

2. Pointed to a own function like
Code: Pascal  [Select][+][-]
  1. function createSomeBitmap: TBGRABitmap;
  2. var tmp : TBGRABitmap;
  3. begin
  4. ...
  5. -->  result := tmp;
  6. end;
  7.  


3. Pointed to lpr
Code: Pascal  [Select][+][-]
  1. --> Application.CreateForm(TForm1, Form1);
  2.  

These infos even apeared after "clean up and compile"

That all seemed to be nonsense.  I activated  valgrind in the debugger  options to see  if I could find some errors. After compiling again and looking into the heaptrc.log, all 3 errors where gone.

Spooky, spooky.

Anybody else with similar experience???

 Lazarus 2.0, fpc 3.04, Suse Tumbleweed

jamie

  • Hero Member
  • *****
  • Posts: 7320
Re: heaptrc - 3 time false positive
« Reply #1 on: April 18, 2019, 12:42:56 am »
I think you'll find the bug is most likely still there, just covered up with the difference of code generation that
takes place with the option you selected, but then again I could be wrong.....


 Recently just prior to 2.0.2 release I found a bug and others helped to locate it and come up a plan to fix it..

The bug is in the TRawImage.LoadFromRawImage call, it actually recreates more chucks of memory when copying the
image and not freeing the original..
 
 Maybe what you are seeing is something in that component getting effective by it?
Put this in the interface section
and this in the
Code: Pascal  [Select][+][-]
  1. TBitmap = Class(Graphics.TBitmap)
  2.  procedure LoadFromRawImage(const AImage: TRawImage; ADataOwner: Boolean);
  3. end;
  4.  
implementation sectect.
Code: Pascal  [Select][+][-]
  1. procedure TBitmap.LoadFromRawImage(const AImage: TRawImage; ADataOwner: Boolean);
  2. var
  3.   img: PRawImage;
  4. begin
  5.   BeginUpdate;
  6.   try
  7.     Clear;
  8.     if AImage.Description.Format = ricfNone then Exit; // empty image
  9.  
  10.     img := GetRawImagePtr;
  11.     img^.Description := AImage.Description;
  12.     if ADataOwner
  13.     then begin
  14.       img^.DataSize := AImage.DataSize;
  15.       img^.Data := AImage.Data;
  16.       img^.MaskSize := AImage.MaskSize;
  17.       img^.Mask := AImage.Mask;
  18.       img^.PaletteSize := AImage.PaletteSize;
  19.       img^.Palette := AImage.Palette;
  20.     end
  21.     else begin
  22.       // copy needed
  23.       img^.DataSize := AImage.DataSize;
  24.       if img^.DataSize > 0
  25.       then begin
  26.         //GetMem(img^.Data, img^.DataSize);
  27.         ReallocMem(img^.Data, img^.DataSize);
  28.         Move(AImage.Data^, img^.Data^, img^.DataSize);
  29.       end
  30.       else img^.Data := nil;
  31.  
  32.       img^.MaskSize := AImage.MaskSize;
  33.       if img^.MaskSize > 0
  34.       then begin
  35.         //GetMem(img^.Mask, img^.MaskSize);
  36.         Reallocmem(img^.Mask, img^.MaskSize);
  37.         Move(AImage.Mask^, img^.Mask^, img^.MaskSize);
  38.       end
  39.       else img^.Mask := nil;
  40.  
  41.       img^.PaletteSize := AImage.PaletteSize;
  42.       if img^.PaletteSize > 0
  43.       then begin
  44.        // GetMem(img^.Palette, img^.PaletteSize);
  45.         ReallocMem(img^.Palette, img^.PaletteSize);
  46.         Move(AImage.Palette^, img^.Palette^, img^.PaletteSize);
  47.       end
  48.       else img^.Palette := nil;
  49.     end;
  50.   finally
  51.     EndUpdate;
  52.   end;
  53.  

I put that in the unit where I was using RawImages.LoadfromRawImage and it fixed the leak

the commented lines are the original, the allocatmem is the added lines.

P.S.
 I guess I didn't submit soon enough, this bug is still in 2.0.2
« Last Edit: April 18, 2019, 12:44:32 am by jamie »
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: heaptrc - 3 time false positive
« Reply #2 on: April 18, 2019, 01:10:14 am »
Hi!

Thank you Jamie for diving deep in the rasterimage.

The strange thing is: after activating the valgrind option once, all the  leaks are gone, even if I disable valgrind again. So there are no memory leaks anymore. And I never found those kind of leaks in 1.6.

Perhaps this Information helps you.

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 18365
  • Here stood a man who saw the Elbe and jumped it.
Re: heaptrc - 3 time false positive
« Reply #3 on: April 18, 2019, 07:54:58 am »
Plz note that your original code did NOT generate false positives: it leaked! although your code is correct.
The cause of the leak is now fixed.

This may not be immediately obvious to other readers.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018