Recent

Author Topic: TATTabs memory leak  (Read 9223 times)

chrv

  • New Member
  • *
  • Posts: 12
TATTabs memory leak
« on: March 22, 2021, 09:32:10 am »
Code below run fine. I can add several tabs at runtime.
Problem when checking for memory leak : 1 unfreed memory block by new tab created.
NB :
- no memory leak if no text is added to the tab
- same hapens with demo : ATFlatControls>app>demo_tabs

What Am i missing ?

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, attabs;
  3. type
  4.   TForm1 = class(TForm)
  5.     Button1: TButton;
  6.     Edit1: TEdit;
  7.     procedure Button1Click(Sender: TObject);
  8.     procedure FormCreate(Sender: TObject);
  9.   private
  10.   public
  11.     AT : TATTabs;
  12.   end;
  13. var
  14.   Form1: TForm1;
  15. implementation
  16. {$R *.lfm}
  17. procedure TForm1.FormCreate(Sender: TObject);
  18. begin
  19.   AT := TATTabs.Create(Form1);
  20.   AT.parent := self;
  21.   AT.Align:=alTop;
  22. end;
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. var
  25.   aTab : TATTabData;
  26. begin
  27.   aTab := TATTabData.Create(AT.Tabs);
  28.   aTab.TabCaption:=Edit1.Text;
  29. end;          
  30.  

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #1 on: March 22, 2021, 11:51:06 am »
aTab := TATTabData.Create(AT.Tabs);

this allocates mem. and you do not free it. normally, I do
  data:= ATTabs1.GetTabData(nIndex);
  data.Caption:= 'smth';

chrv

  • New Member
  • *
  • Posts: 12
Re: TATTabs memory leak
« Reply #2 on: March 22, 2021, 12:36:06 pm »
Thanks for quick reply.

Your code is fine to change Tab caption but what about (dynamically) creating a new tab  (like in my little example program) ?

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #3 on: March 22, 2021, 02:02:03 pm »
Method TATTabs.AddTab will do that alloc of Data.

chrv

  • New Member
  • *
  • Posts: 12
Re: TATTabs memory leak
« Reply #4 on: March 22, 2021, 03:08:31 pm »
I coverted my little demo with your suggestion.
It seems it does not change anything. Still memory leak

heaptrc : 1 unfreed memory blocks

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, attabs;
  6. type
  7.   TForm1 = class(TForm)
  8.     Button1: TButton;
  9.     Edit1: TEdit;
  10.     procedure Button1Click(Sender: TObject);
  11.     procedure FormCreate(Sender: TObject);
  12.   private
  13.   public
  14.     AT : TATTabs;
  15.   end;
  16.  
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21. {$R *.lfm}
  22. procedure TForm1.FormCreate(Sender: TObject);
  23. begin
  24.   AT := TATTabs.Create(Form1);
  25.   AT.parent := self;
  26.   AT.Align:=alTop;
  27. end;
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. //var
  30. //  aTab : TATTabData;
  31. begin
  32.   AT.AddTab(-1,Edit1.Text);
  33.   //aTab := TATTabData.Create(AT.Tabs);
  34.   //aTab.TabCaption:=Edit1.Text;
  35. end;
  36. end.
     

More information :
1) if  Edit.text ='' => no memory leak   
2) AT.AddTab(-1,'XXX') => no memory leak

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #5 on: March 22, 2021, 03:18:56 pm »
According to your info,I added deallocation of Data's. Pls get GitHub version and test?

chrv

  • New Member
  • *
  • Posts: 12
Re: TATTabs memory leak
« Reply #6 on: March 22, 2021, 07:30:53 pm »
For me, it still leaking
Code: Pascal  [Select][+][-]
  1. destructor TATTabListCollection.Destroy;
  2. var
  3.   i : integer;
  4. begin
  5.   For i := Count-1 downto 0 do
  6.     Items[i].Free;
  7.   inherited Destroy;
  8. end;  
It seems Count allready = 0 at this point.

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #7 on: March 22, 2021, 08:39:23 pm »
Yes, Count is 0, changed that place.
Leaking: I don't have an idea.

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #8 on: March 22, 2021, 08:49:27 pm »
I run the demo_tabs from ATFlatControls, with 'heaptrc' checkbox in IDE options, and see NO leaks.
Quote
Heap dump by heaptrc unit of /home/user/cuda/atflatcontrols/app/demo_tabs/demo
12747 memory blocks allocated : 27977489/27999088
12747 memory blocks freed     : 27977489/27999088
0 unfreed memory blocks : 0
True heap size : 2686976
True free heap : 2686976
 

this is after fixing that demo_tabs.

tom.tom

  • New Member
  • *
  • Posts: 43
Re: TATTabs memory leak
« Reply #9 on: July 22, 2021, 04:32:26 pm »
Yeah, there is something strange with memory leak when assigning the dynamic string to the TabCaption property. It looks like a false positive report though...

No leak reported:
Code: Pascal  [Select][+][-]
  1.   for ids := 1 to CameraTabs.TabCount do
  2.   begin
  3.     tabData := CameraTabs.GetTabData(Pred(idx));
  4.     tabData.TabCaption := 'test';
  5.   end;
  6.  

[TabCount-1] leaks reported where the TabCaption is assigned:
Code: Pascal  [Select][+][-]
  1.   for idx:= 1 to CameraTabs.TabCount do
  2.   begin
  3.     tabData := CameraTabs.GetTabData(Pred(idx));
  4.     tabData.TabCaption := Format('%d: %s', [idx, 'test']);;
  5.   end;
  6.  

Isn't it because heaptrc gets confused because tabData is just a reference and never freed locally?


Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #10 on: July 22, 2021, 07:59:04 pm »
I tried to clear TabList collection in Destroy - updated ATTabs in GitHub. Pls test.

chrv

  • New Member
  • *
  • Posts: 12
Re: TATTabs memory leak
« Reply #11 on: July 22, 2021, 11:52:35 pm »
I never succeded to fine a working solution with TATTabs : allways memory leaks when dynamicaly creating Tabs.
Finaly i tested TJvTabBar (from Jvcllaz in Online Package Manager) ) : everything works without problem (no memory leaks).

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #12 on: July 22, 2021, 11:54:13 pm »
@chrv,
ATTabs has a patch from today, to fix the leaks, pls try.

tom.tom

  • New Member
  • *
  • Posts: 43
Re: TATTabs memory leak
« Reply #13 on: July 23, 2021, 05:02:29 pm »
Still leaks (see the attachment).

Line 7709 of Unit1 is dynamic string assignment to the TabCaption property.

Alextp

  • Hero Member
  • *****
  • Posts: 1377
    • UVviewsoft
Re: TATTabs memory leak
« Reply #14 on: July 23, 2021, 07:01:43 pm »
An idea of 'additional leak' is that you assign TATTabData.TabObject, but you don't free it.
Or are you sure it's leak of 'string' vars?

 

TinyPortal © 2005-2018