Recent

Author Topic: [SOLVED] TBitmap.Create causing Windows memory leak.  (Read 603 times)

petevick

  • Sr. Member
  • ****
  • Posts: 337
[SOLVED] TBitmap.Create causing Windows memory leak.
« on: April 19, 2023, 12:53:38 pm »
I'm using the following code to create a bitmap of the app, but on closing the app I get a memory leak report indicating that the problem is with the imgWindow := TBitmap.Create;
I've tried to change imgWindow.Free; to imgWindow.Destroy; but still get the memory leak.

Any help would be gratefully received.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   imgWindow: TBitmap;
  4. begin
  5.   imgWindow := TBitmap.Create;
  6.   try
  7.     imgWindow := Form1.GetFormImage;
  8.     imgWindow.SaveToFile('c:\FormImage.bmp');
  9.   finally
  10.     imgWindow.Free;
  11.   end;
  12. end;
  13.  
« Last Edit: April 19, 2023, 04:25:13 pm by petevick »
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: TBitmap.Create causing Windows memory leak.
« Reply #1 on: April 19, 2023, 12:58:20 pm »
Delete line 5.

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TBitmap.Create causing Windows memory leak.
« Reply #2 on: April 19, 2023, 01:00:55 pm »
Delete line 5.
Brilliant, thanks bytebites  ;)
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: TBitmap.Create causing Windows memory leak.
« Reply #3 on: April 19, 2023, 01:03:50 pm »
But don't use Form1 in the code of the class TForm1. This way your code works only when the instance of TForm1 that you created is named "Form1".
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   imgWindow: TBitmap;
  4. begin
  5.   imgWindow := GetFormImage;  // wrong: Form1.GetFormImage;
  6.   try
  7.     imgWindow.SaveToFile('c:\FormImage.bmp');
  8.   finally
  9.     imgWindow.Free;
  10.   end;
  11. end;

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TBitmap.Create causing Windows memory leak.
« Reply #4 on: April 19, 2023, 01:08:53 pm »
But don't use Form1 in the code of the class TForm1. This way your code works only when the instance of TForm1 that you created is named "Form1".
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   imgWindow: TBitmap;
  4. begin
  5.   imgWindow := GetFormImage;  // wrong: Form1.GetFormImage;
  6.   try
  7.     imgWindow.SaveToFile('c:\FormImage.bmp');
  8.   finally
  9.     imgWindow.Free;
  10.   end;
  11. end;
Noted, and thanks wp  ;D
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018