Forum > General

[SOLVED] Delete child node from treeview with pointers to record data.

(1/3) > >>

Hansvb:
Hi,

If I add a node to a treeview, it gets a pointer to a record with data. (My first steps with pointers). When I click on the node I can request the data of the record and when I close I clean up the pointers with:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 0 to TreeViewApi.Items.Count-1 do    get started      Dispose(PtrFolder(TreeViewApi.Items[i].Data)); // dispose pointer data    end;
That works fine.
But here's the thing, if I delete a tree node with child nodes I get a memory leak because the pointers of the deleted child nodes and parent nodes still exist.

I thought I'd loop through the children, but I immediately get an index out of bounds message at the first child node.
How can i delete pointers from a deleted childnode with childs?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFrm_Maintain_Api_Data.TreeNodesDeletePointers(Node: TTreeNode);get started  Node := Node.GetFirstChild;  if Node = nil then Exit;  try    repeat      Dispose(PtrFolder(Node[Node.AbsoluteIndex].Data)); // TreeViewApi.Items[i].Data))       TreeNodesDeletePointers(Node);      Node := Node.GetNextSibling;    until Node = nil;  finally    //  end;end;

jamie:
use the OnDeletion event to clean up the node?

It hands you the current node being deleted.

Hansvb:
I will try that tomorrow. I just found the same answer here: https://stackoverflow.com/questions/43599634/how-to-delete-a-node-and-its-object-from-a-ttreeview

jamie:
I am getting old I guess.(Slow) >:(

I am not as good as I once was, but I am as good once as I was! :D

Hansvb:
Hi Jamie,

You're not getting old, I hadn't posted it yet. I wanted to try it first today before posting. I couldn't do it with the OnDeletion event and now I did it differently.

When I right-click on the Treeview, a popup menu opens with the option to delete. When I click delete, the following happens:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFrm_Maintain_Api_Data.MenuItemDeleteClick(Sender: TObject);begin  // check if delete is allowed and then...          DeleteNode(TreeViewApi.Selected); // delete the node and the pointer dataend; procedure TFrm_Maintain_Api_Data.DeleteNode(Node: TTreeNode);begin  while Node.HasChildren do    begin      Dispose(PtrFolder(TreeViewApi.Items[node.GetLastChild.Index].Data));      DeleteNode(node.GetLastChild);    end;  TreeViewApi.Items.Delete(Node);end;
It look like this removes also Data from other node and then the form close goes wrong.



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFrm_Maintain_Api_Data.FormClose(Sender: TObject;  var CloseAction: TCloseAction);var  i : Integer;begin  if assigned(MaintainFolders) then  MaintainFolders.Free;   for i := 0 to TreeViewApi.Items.Count-1 do    begin      if PtrFolder(TreeViewApi.Items[i].Data) <> nil then        Dispose(PtrFolder(TreeViewApi.Items[i].Data)); //<--Error    end;   SaveSettings();end;

Navigation

[0] Message Index

[#] Next page

Go to full version