Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Does Treeview Data Objects need to be freed?

Delphi 2010 - I have a Treeview with approx 2000 nodes. Each node has a Data Object, which points to a record. When I exit my program, does the memory associated with the Data objects get automatically freed, or do I need to traverse each node a DISPOSE of them?

like image 766
user1009073 Avatar asked Nov 15 '25 19:11

user1009073


1 Answers

Having found this question via a Google search and not finding the solution I have just used, I have decided to put an answer even though the question is a couple of years old.

TTreeView has an OnTreeDeletion method which is called as each node in the tree is deleted giving the opportunity to free/dispose of any associated data.

Here is my code:

procedure TfmMyForm.MyTreeDeletion(Sender: TObject; Node: TTreeNode);
var
  rci: TRecentCodeItem;
begin
  rci := node.data;

  if assigned(rci) then
    freeandnil(rci);
end;

I store a pointer to an object of type TRecentCodeItem with the node.

One difference between my situation and the questioner's is that my tree view was not on the main form so I couldn't just leave it to windows to mop up the memory after the application closes. Not that I would do that anyway - I do like to have zero memory leaks.

like image 160
Michael Vincent Avatar answered Nov 18 '25 21:11

Michael Vincent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!