Recent

Author Topic: Using FV's TListBox, Memory Leak,  (Read 996 times)

gasensor

  • New Member
  • *
  • Posts: 15
Using FV's TListBox, Memory Leak,
« on: May 02, 2022, 07:16:22 pm »
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
« Last Edit: May 18, 2022, 09:51:29 pm by gasensor »

Mathias

  • Jr. Member
  • **
  • Posts: 78
Re: Using FeeVision TListBox, Memory Leak,
« Reply #1 on: May 22, 2022, 05:30:06 pm »
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  [Select][+][-]
  1. constructor TMyDialog.Init;
  2. var
  3.   Rect: TRect;
  4.   ScrollBar: PScrollBar;
  5.   i: Sw_Integer;
  6.   rad: PRadioButtons;
  7.   bp:PButton;
  8. const
  9.   Tage: array [0..6] of shortstring = (
  10.     'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag');
  11.  
  12. begin
  13.   Rect.Assign(10, 5, 67, 17);
  14.   inherited Init(Rect, 'ListBox Demo');
  15.  
  16.   rad := New(PRadioButtons, Init(Rect, NewSItem('~G~ross', NewSItem('~M~ittel', NewSItem('~K~lein', nil)))));
  17.   Dispose(rad, Done); // io.
  18.  
  19.   bp := New(PButton, Init(Rect, '~T~ag', cmTag, bfNormal));
  20.   Dispose(bp, Done); // io.
  21.   exit;

But if I do the same with PListBox, there are memory corpses.
Code: Pascal  [Select][+][-]
  1.   ListBox := new(PListBox, Init(Rect, 1, nil));
  2.   Dispose(ListBox, Done);  // Memory Leak !
  3.   exit;



Could there be a bug in PListBox?


« Last Edit: May 22, 2022, 05:33:52 pm by Mathias »

Mathias

  • Jr. Member
  • **
  • Posts: 78
Re: Using FV's TListBox, Memory Leak,
« Reply #2 on: May 23, 2022, 06:36:27 pm »
I took a closer look at the TFileListBox used by the TFileDialog. Then I came across the following:

Code: Pascal  [Select][+][-]
  1. destructor TFileList.Done;
  2. begin
  3.   if List <> nil then Dispose(List, Done);
  4.   TListBox.Done;
  5. end;
  6.  
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  [Select][+][-]
  1. type
  2.   PNewListBox = ^TNewListBox;
  3.   TNewListBox = object(TListBox)
  4.     destructor Done; virtual;
  5.   end;
  6. ...
  7. destructor TNewListBox.Done;
  8. begin
  9.   if List <> nil then begin
  10.     Dispose(List, Done);
  11.   end;
  12.   TListBox.Done;
  13. end;
  14. constructor TMyDialog.Init;
  15. var
  16.   Rect: TRect;
  17.   ScrollBar: PScrollBar;
  18.   i: Sw_Integer;
  19. const
  20.   Tage: array [0..6] of shortstring = (
  21.     'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag');
  22.  
  23. begin
  24.   Rect.Assign(10, 5, 67, 17);
  25.   inherited Init(Rect, 'ListBox Demo');
  26.  
  27.   // StringCollection
  28.   StringCollection := new(PStringCollection, Init(5, 5));
  29.   for i := 0 to Length(Tage) - 1 do begin
  30.     StringCollection^.Insert(NewStr(Tage[i]));
  31.   end;
  32.  
  33.   // ScrollBar für ListBox
  34.   Rect.Assign(31, 2, 32, 7);
  35.   ScrollBar := new(PScrollBar, Init(Rect));
  36.   Insert(ScrollBar);
  37.  
  38.   // ListBox
  39.   Rect.Assign(5, 2, 31, 7);
  40.   ListBox := new(PNewListBox, Init(Rect, 1, ScrollBar));
  41.   ListBox^.NewList(StringCollection);
  42.   Insert(ListBox);
  43.  
  44.   // Cancel-Button
  45.   Rect.Assign(19, 9, 32, 10);
  46.   Insert(new(PButton, Init(Rect, '~T~ag', cmTag, bfNormal)));
  47.  
  48.   // Ok-Button
  49.   Rect.Assign(7, 9, 17, 10);
  50.   Insert(new(PButton, Init(Rect, '~O~K', cmOK, bfDefault)));
  51. end;

Mathias

  • Jr. Member
  • **
  • Posts: 78
Re: Using FV's TListBox, Memory Leak,
« Reply #3 on: May 30, 2022, 05:21:45 pm »
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.

 

TinyPortal © 2005-2018