0

Is there a straight-forward way to figure if a treenode was deleted? A property or a function being unanimously nil, -1, false, or so?

// tn:ttreenode; tvyu:ttreeview;
tn:=tvyu.items[4]; // tn is nothing but a pointer, i guess
// do something including:
tvyu.items[4].delete; // this probably does free some memory
// do something then check if tn was deleted
if (tn.absoluteindex=0) and (tn<>tvyu.items[0]) then ... // this works at first glance but
// i guess tn just points to a zero randomly and i can`t rely on it

I could check all variables of this type and nil them in the deletion event if they match, just wondering if there is a simpler method.

miodrag
  • 99
  • 10
  • 1
    Put the node in a "deleted" collection prior to calling `.delete` – Robert Harvey Feb 21 '19 at 20:30
  • Nope. You can't possibly tell if an arbitrary reference refers to a destroyed object. – David Heffernan Feb 21 '19 at 21:23
  • `TTreeView` has an `OnDeletion` event when a `TTreeNode` is about to be destroyed. Use it to update external references to nodes as needed – Remy Lebeau Feb 21 '19 at 22:21
  • Related: https://stackoverflow.com/questions/8598408/how-to-detect-dangling-pointers-if-assigned-cant-do-it You shouldn't use a tree view to "store" your data. Only "display" it to the user. Your primary storage should be a separate non-visual structure. – Jerry Dodge Feb 21 '19 at 22:40
  • @David, i thought that might be the case, but i wasn't sure - thanks for stating it explicitly. And thanks everyone for the discussion. Robert, your solution is better for speed optimization, whereas Remy's and mine (that's what i wrote in my last paragraph) is better for memory optimization. Jerry, i disagree. I use nodes to store data and had not ran into problems so far. Deletion event can take care of anything needing disposal. So far these events have been executing as expected. – miodrag Feb 22 '19 at 20:44
  • @jerry is correct. As a principle, don't use a UI control S primary data store. Use it to view data. – David Heffernan Feb 23 '19 at 01:41

0 Answers0