I am using a TTreeView control which has a Data pointer for each item.
How do you determine if this pointer points to an object ?
ta,
S
There'll only be an object pointed to if you set Data yourself; otherwise
you can set it to Nil to indicate that there's no object (I think it
defaults to Nil anyway, but why take chances?). If you free the object
before the tree node's removed, set Data back to Nil to reflect this.
Ian
It could be implied from your answer that the object is freed
automatically when the node is removed.
I would just like to ensure that impression is not reached. Also a good
place to free an object is via the OnDeletion event handler, which fires
for each node.
Mike Orriss (TeamB and DevExpress)
>> If you free the object before the tree node's removed, set Data back
>> to Nil to reflect this.
MOT> It could be implied from your answer that the object is freed
MOT> automatically when the node is removed.
I wonder why FreeAndNil functionality is not embedded into .Free method and
into Dispose/FreeMem procedures.
With best regards, . E-mail:
Good point Mike, that certainly wasn't my intent, but I can see how it could
easily be read that way!
Ian
if TreeView.Selected.Data <> nil then // if the pointer points an object
begin
.....
end
else // if the data pointer was not assigned
begin
..
end;
"Sean" <Mc...@Consultant.com> schrieb im Newsbeitrag
news:3c7e0d0e_1@dnews...
Please keep spamming my email account. I don't mind because I don't read it.
"Cure Spammers for ever, Shoot the b*st**ds" Tony
But doesn't Free do exactly that: disposes the memory (a.m.o.t), but keeping
"self" pointing at it?
Because very often, the programmer will have many things pointing to the
same object in memory. It is wrong for any component to assume it owns
anything it points to and can delete them at will, since the object being
pointed to may have a broader scope than that of the component pointing to
it.
-Gary
I was referring to components such as TListView, TTreeView, TListBox, etc.
which can have objects attached to items at run-time. I mis-read the
question. I thought the poster was asking why TListView, etc. didn't
automatically destroy the objects that were attached to it at run-time.
-Gary
Because it can't. An object does not know anything about the pointer to
itself. Besides, there may be more than one pointer.
That is not foolproof.
After:
TObject(TreeView.Selected.Data).Free;
Then Data still points to where the object used to be. If there is any
possibility of Data being referenced again, it must be set to nil.
Matheus
"Mike Orriss (TeamB)" <m...@3kcc.co.uk> ha scritto nel messaggio
news:VA.00002981.096aeeb2@pcmike1...
Rather a large overhead when it affects *all* objects. Even then it would
not cope with multiple references to the object.