Board index » delphi » Pointer memory and memory leaking

Pointer memory and memory leaking

I'm using TTreeNode.AddObject this way:

      NewNode:=AddObject(ParentNode,'Text for this node',MyInt);

where MyInt is an integer variable.

When I do a TreeView1.NewNode.Delete, do I have a memory leak?

The help file says:
=====
AddObject adds a new node containing data to a tree view.

function AddObject(Node: TTreeNode; const S: string; Ptr: Pointer):
TTreeNode;

Description

The node is added as the last sibling of the node specified by the Node
parameter. The S parameter specifies the Text property of the new node.
The Ptr parameter specifies the Data property value of the new node.
AddObject returns the node that has been added.

Note

The memory referenced by Ptr is not freed when the tree nodes object is
freed.
=====

 

Re:Pointer memory and memory leaking


Quote
Alan MacArthur wrote:

> I'm using TTreeNode.AddObject this way:

>       NewNode:=AddObject(ParentNode,'Text for this node',MyInt);

> where MyInt is an integer variable.

I'd firmly put this in the land of dodgy programming. I hope the 777 fly
by wire system ain't written like this <grin>. You really *should*
supply a pointer to something. Although ints happen to be the same size
as pointers, this isn't necessarily the case.

Quote
> When I do a TreeView1.NewNode.Delete, do I have a memory leak?

No, because you haven't allocated any memory and subsequently failed to
free it later.

Quote

> Note

> The memory referenced by Ptr is not freed when the tree nodes object is
> freed.

True. However, If the pointer doesn't reference memory but simply
garbage, being an integer in disguise, then that does not apply.

I would really suggest you do in fact use these things as pointers in
the correct manner. It's more work, but extendible and cleaner.

MH.

--
Martin Harvey.
mc...@harvey27.demon.co.uk
http://www.harvey27.demon.co.uk/mch24/

Re:Pointer memory and memory leaking


Quote
Alan MacArthur wrote:

> I'm using TTreeNode.AddObject this way:

>       NewNode:=AddObject(ParentNode,'Text for this node',MyInt);

> where MyInt is an integer variable.

You don't. AddObject is declared with the 3rd parameter as a Pointer. So you
do a typecast at least. On the other side, this is a common way to store
integer values. Since you don't allocate memory for that pseudo-pointer, there
is no need to free it (Better: You must not free it or You have nothing to
free). And you do not get a memory leak.

HTH
-Michael

Re:Pointer memory and memory leaking


Martin Harvey heeft geschreven in bericht
<37D802EF.C66EE...@harvey27.demon.co.uk>...

Quote
>Alan MacArthur wrote:

>> I'm using TTreeNode.AddObject this way:

>>       NewNode:=AddObject(ParentNode,'Text for this node',MyInt);

>> where MyInt is an integer variable.

>I'd firmly put this in the land of dodgy programming. I hope the 777 fly
>by wire system ain't written like this <grin>. You really *should*
>supply a pointer to something.

...not if you really only need to store a (typecasted) integer value, for
instance an index into a related table, stringlist or whatever. The same
goes for the objects[] property of TStringList, TListbox & al. Dodgy? No, I
don't think so...

Regards,
Dirk Claessens
<Dirk DOT Claessens AT Village DOT uunet DOT be>
Sorry, mailheader is forged...

Re:Pointer memory and memory leaking


Quote
Dirk Claessens wrote:

> Martin Harvey heeft geschreven in bericht
> <37D802EF.C66EE...@harvey27.demon.co.uk>...
> >Alan MacArthur wrote:

> >> I'm using TTreeNode.AddObject this way:

> >>       NewNode:=AddObject(ParentNode,'Text for this node',MyInt);

> >> where MyInt is an integer variable.

> >I'd firmly put this in the land of dodgy programming. I hope the 777 fly
> >by wire system ain't written like this <grin>. You really *should*
> >supply a pointer to something.

> ...not if you really only need to store a (typecasted) integer value, for
> instance an index into a related table, stringlist or whatever. The same
> goes for the objects[] property of TStringList, TListbox & al. Dodgy? No, I
> don't think so...

> Regards,
> Dirk Claessens
> <Dirk DOT Claessens AT Village DOT uunet DOT be>
> Sorry, mailheader is forged...

Just a note, while copying and editing that code to minimize it for this
posting, I inadvertently removed the Pointer() that was around MyInt.
Sorry for any confusion.

Alan MacArthur

Other Threads