Forum > FV/Textmode IDE

Using FV's TListBox, Memory Leak,

(1/1)

gasensor:
I am Using FV's TListBox. But find it Memory Leaked. It has been bothering me for a long time.

sechshelme 's "FPC FreeVision Tutorial"  do not have a  TListBox DEMO...

Can anyone give me an example of how to use TLISTBOX properly?

TKS

Mathias:
After trying back and forth for a long time, this unpleasant memory corpse remains with TList.
I have described all the findings in my GitHub Issues channel.
https://github.com/sechshelme/Lazarus-FreeVision/issues/3

I tried out several components, where I created them and then released them again with Dispose/Done.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---constructor TMyDialog.Init;var  Rect: TRect;  ScrollBar: PScrollBar;  i: Sw_Integer;  rad: PRadioButtons;  bp:PButton;const  Tage: array [0..6] of shortstring = (    'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'); begin  Rect.Assign(10, 5, 67, 17);  inherited Init(Rect, 'ListBox Demo');   rad := New(PRadioButtons, Init(Rect, NewSItem('~G~ross', NewSItem('~M~ittel', NewSItem('~K~lein', nil)))));  Dispose(rad, Done); // io.   bp := New(PButton, Init(Rect, '~T~ag', cmTag, bfNormal));  Dispose(bp, Done); // io.  exit;
But if I do the same with PListBox, there are memory corpses.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  ListBox := new(PListBox, Init(Rect, 1, nil));  Dispose(ListBox, Done);  // Memory Leak !  exit;


Could there be a bug in PListBox?


Mathias:
I took a closer look at the TFileListBox used by the TFileDialog. Then I came across the following:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---destructor TFileList.Done;begin  if List <> nil then Dispose(List, Done);  TListBox.Done;end; They added their own destructor there.

Now I also built a new ListBox and added a destructor and used this ListBox in my dialog.
As it seems, this works. the memory leak is gone.

I'll make a few more tries.
In my opinion, this is a bug in TListBox, which lacks the destructor.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  PNewListBox = ^TNewListBox;  TNewListBox = object(TListBox)    destructor Done; virtual;  end;...destructor TNewListBox.Done;begin  if List <> nil then begin    Dispose(List, Done);  end;  TListBox.Done;end;constructor TMyDialog.Init;var  Rect: TRect;  ScrollBar: PScrollBar;  i: Sw_Integer;const  Tage: array [0..6] of shortstring = (    'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'); begin  Rect.Assign(10, 5, 67, 17);  inherited Init(Rect, 'ListBox Demo');   // StringCollection  StringCollection := new(PStringCollection, Init(5, 5));  for i := 0 to Length(Tage) - 1 do begin    StringCollection^.Insert(NewStr(Tage[i]));  end;   // ScrollBar für ListBox  Rect.Assign(31, 2, 32, 7);  ScrollBar := new(PScrollBar, Init(Rect));  Insert(ScrollBar);   // ListBox  Rect.Assign(5, 2, 31, 7);  ListBox := new(PNewListBox, Init(Rect, 1, ScrollBar));  ListBox^.NewList(StringCollection);  Insert(ListBox);   // Cancel-Button  Rect.Assign(19, 9, 32, 10);  Insert(new(PButton, Init(Rect, '~T~ag', cmTag, bfNormal)));   // Ok-Button  Rect.Assign(7, 9, 17, 10);  Insert(new(PButton, Init(Rect, '~O~K', cmOK, bfDefault)));end;

Mathias:
So, now a tutorial for ListBoxes has been added.
With the ListBoxes, you can manually remove the loaded list from memory. This is described in the tutorial.

Navigation

[0] Message Index

Go to full version