[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Does TTreeNode.Data need to be disposed of?

Posted on 2002-03-25
7
Medium Priority
?
491 Views
Last Modified: 2008-02-26
Hi, I am populating TreeView with File Names and I use
the TTreeNode.Data property to point at strings
containing each File Name's path.

procedure TForm1.GetFiles(tree:TTreeView; pn: TTreeNode; fp:string);
//pn=parent node, fp=folder path, tn=temp node
var sr: TSearchRec;
    ps: ^string;
    tn: TTreeNode;
begin
  AddLastSlash(fp);
  if FindFirst(fp + '*.jpg', faArchive, sr) = 0 then
  begin
    repeat
      if ((sr.Attr and faArchive) = faArchive) then
      begin

        new(ps);
        ps^:= fp + sr.name;
        tn := tree.Items.AddChildObject(pn, sr.Name, ps);

        tn.ImageIndex := 6;
        tn.SelectedIndex := 6;
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

My question is:  Do I have to call Dispose(Node.Data)
before I use Node.Delete, Node.DeleteChildren or
Tree.Items.Clear methods?  

I tried it with and without Dispose, there seems to be no
problem either way, so what is the right way?
Thanks
cj
0
Comment
Question by:czechmate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:Fraction
ID: 6895007
I don't think the dispose memory for node data is done automaticly when you delete a node, but when your application terminates I think it disposes all allocated memory. But a simple way to do this is to add the dispose memory code to the TreeView Deletion event.

procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
 Dispose(Node.Data);
end;
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6895015
I don't think the dispose memory for node data is done automaticly when you delete a node, but when your application terminates I think it disposes all allocated memory. But a simple way to do this is to add the dispose memory code to the TreeView Deletion event.

procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
 Dispose(Node.Data);
end;
0
 
LVL 1

Author Comment

by:czechmate
ID: 6895031
Thanks Fraction, I know about this event I've used it.  
The question really is does one have to do it or not :)

Delphi Help says:
When an application is finished using a dynamic variable
created with New, it should dispose of the memory allocated for the variable using the Dispose standard procedure.

I would think that if it is used with parent-less
structures like linked lists than yes.  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Russell Libby
ID: 6895067

The simple answer is yes.
The Delphi compiler makes no assumptions about the contents held in the the Data property, other than the fact its a pointer. (it could be a pointer to an object,  record type, record type with pointers to allocated memory, etc..). So you, as the programmer, are left with the resposibility of releasing any allocated memory.

Russell
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6895082
I think you can ignore disposing memory, and run your application without any problem for some time, but you can't assume that the user should exit your application frequently just to free memory. If you keep adding and removing nodes to your tree view sooner or later you will run out of memory.
0
 

Accepted Solution

by:
classics earned 150 total points
ID: 6895567
In the destructor for TTreeNode, Data is simply set to Nil without checking its value.  Anything you associate with this pointer should be freed before destroying the node.
0
 
LVL 1

Author Comment

by:czechmate
ID: 6895713
To Classics:
I am going to accept your proposed answer since it looks technically correct.  However, I would like to remind you, it is much more friendy to post your response as a comment and thus give chance to other experts to contribute or voice their opinion.  
Regards
cj
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question