View Issue Details

IDProjectCategoryView StatusLast Update
0035372LazarusLCLpublic2019-05-11 13:33
Reporterjamie philbrookAssigned ToMartin Friebe 
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Platformmost likely allOSFound on Windows but not relatedOS Version10
Product Version2.0Product BuildCurrent release. 
Target VersionFixed in Version2.0.4 
Summary0035372: Tbitmap.LoadFromRawImage creates memory leaks of image size.
DescriptionIf you use the Tbitmap.LoadFromRawImage it recreates the image and
does not clean up the old one, leaving behind memory when freeing the
object.

See Post
https://forum.lazarus.freepascal.org/index.php/topic,45016.0.html
Steps To ReproduceEDITED BY MFR:

Code to reproduce, from forum:

    procedure TForm1.Button1Click(Sender: TObject);
    Var
      A,B:TBitmap;
    begin
      A := Tbitmap.Create; B := Tbitmap.Create;
      A.PixelFormat := pf24bit; B.PixelFormat := pf24bit;
      A.SetSize(320,240); B.SetSize(320, 240);
      A.Canvas.Changed; //Force a DIB GUI type in windows.
      A.LoadFromRawImage(B.RawImage,false); //Keep B as RAW only..
      A.Free;
      B.Free;
    end;

Below suggested fix from reporter (procedure is in TRasterImage, not TBitmap)
----

See posted articles and fixes that I have tested and works on my end...

I didn't recompile or rebuild the LCL for this fix, I simply tested with
a local override of the procedure..
procedure TBitmap.LoadFromRawImage(const AImage: TRawImage; ADataOwner: Boolean);
var
  img: PRawImage;
begin
  BeginUpdate;
  try
    Clear;
    if AImage.Description.Format = ricfNone then Exit; // empty image

    img := GetRawImagePtr;
    img^.Description := AImage.Description;
    if ADataOwner
    then begin
      img^.DataSize := AImage.DataSize;
      img^.Data := AImage.Data;
      img^.MaskSize := AImage.MaskSize;
      img^.Mask := AImage.Mask;
      img^.PaletteSize := AImage.PaletteSize;
      img^.Palette := AImage.Palette;
    end
    else begin
      // copy needed
      img^.DataSize := AImage.DataSize;
      if img^.DataSize > 0
      then begin
        //GetMem(img^.Data, img^.DataSize);
        ReallocMem(img^.Data, img^.DataSize);
        Move(AImage.Data^, img^.Data^, img^.DataSize);
      end
      else img^.Data := nil;

      img^.MaskSize := AImage.MaskSize;
      if img^.MaskSize > 0
      then begin
        //GetMem(img^.Mask, img^.MaskSize);
        Reallocmem(img^.Mask, img^.MaskSize);
        Move(AImage.Mask^, img^.Mask^, img^.MaskSize);
      end
      else img^.Mask := nil;

      img^.PaletteSize := AImage.PaletteSize;
      if img^.PaletteSize > 0
      then begin
       // GetMem(img^.Palette, img^.PaletteSize);
        ReallocMem(img^.Palette, img^.PaletteSize);
        Move(AImage.Palette^, img^.Palette^, img^.PaletteSize);
      end
      else img^.Palette := nil;
    end;
  finally
    EndUpdate;
  end;
TagsNo tags attached.
Fixed in Revision61202
LazTarget2.0.4
Widgetset
Attached Files

Activities

jamie philbrook

2019-04-13 15:59

reporter   ~0115473

The commented GETMEM and following ReallocMem lines is what has been done
to fixed

 GetRawImagePtr returns an already allocated image.

Martin Friebe

2019-05-11 13:29

manager   ~0116129

Edited steps to reproduce

Martin Friebe

2019-05-11 13:33

manager   ~0116130

Please test, and close if ok

Issue History

Date Modified Username Field Change
2019-04-13 15:56 jamie philbrook New Issue
2019-04-13 15:59 jamie philbrook Note Added: 0115473
2019-05-11 13:29 Martin Friebe Description Updated View Revisions
2019-05-11 13:29 Martin Friebe Steps to Reproduce Updated View Revisions
2019-05-11 13:29 Martin Friebe LazTarget => -
2019-05-11 13:29 Martin Friebe Note Added: 0116129
2019-05-11 13:32 Martin Friebe Assigned To => Martin Friebe
2019-05-11 13:32 Martin Friebe Status new => assigned
2019-05-11 13:33 Martin Friebe Status assigned => resolved
2019-05-11 13:33 Martin Friebe Resolution open => fixed
2019-05-11 13:33 Martin Friebe Fixed in Version => 2.0.4
2019-05-11 13:33 Martin Friebe Fixed in Revision => 61202
2019-05-11 13:33 Martin Friebe LazTarget - => 2.0.4
2019-05-11 13:33 Martin Friebe Note Added: 0116130