Recent

Author Topic: Treeview memory leak  (Read 3392 times)

wp

  • Hero Member
  • *****
  • Posts: 13268
Treeview memory leak
« on: February 15, 2016, 01:24:26 am »
In the simple demo project that I posted for another topic (http://forum.lazarus.freepascal.org/index.php/topic,31521.msg202076.html#msg202076) heaptrc reports a bunch of memory leaks although I am pretty sure that created objects are properly destroyed.

In this demo, instances of a simple class are attached to the Data pointer of the tree nodes. The data are destroyed in the OnDeletion event of the treeview.

To check load the attachment from http://forum.lazarus.freepascal.org/index.php/topic,31521.msg202076.html#msg202076 and enable the heaptrc option in the project options. Run, click "Add", enter some dummy data, "OK", and close the program. --> heaptrc reports more than 300 unfreed memory blocks! It is strange that none of the reported leaking lines is in my code.

Has anybody else seen this?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Treeview memory leak
« Reply #1 on: February 15, 2016, 01:54:59 am »
The leak doesn't come from TreeView or its data but from Form2. Adding line
Code: Pascal  [Select][+][-]
  1. F.Free;
to the end of method Unit2.CreateData resolves problem. That's because F is created without owner -> noone frees it.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Treeview memory leak
« Reply #2 on: February 15, 2016, 02:01:07 am »
Code: Pascal  [Select][+][-]
  1. function CreateData: TMyData;
  2. var
  3.   F: TForm2;
  4. begin
  5.   F := TForm2.Create(nil);
  6.   try
  7.     if F.ShowModal = mrOK then begin
  8.       Result := TMyData.Create;
  9.       Result.FName := F.EdName.Text;
  10.       Result.FAddress := F.EdAddress.Text;
  11.       Result.FCity := F.EdCity.Text;
  12.       Result.FBirthday := F.EdBirthday.Date;
  13.     end else
  14.       Result := nil;
  15.   finally
  16.     F.Free;
  17.   end;
  18. end;
  19.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 13268
Re: Treeview memory leak
« Reply #3 on: February 15, 2016, 10:16:57 am »
Thank you guys. I should not post code that I wrote too late at night...

 

TinyPortal © 2005-2018