[SOLVED] Free TTreeView data pointer problems lazarus / 31-LCL ---------------------------------------------------------------------- JD · 2016-05-29 12:40 Hi there everyone, I have a treeview that I populate normally and add extra data to it through its Data property. I use the class below: Code: [Select]  { TTreeData }   PTreeData = ^TTreeData;   TTreeData = class   private     FID: string;   published     property ID: string read FID write FID;   end; I populate the treeview using the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // Fill the listview from the JSON string  ATreeVu.Items.BeginUpdate;  // Clear the listview items as well as any data found in the nodes  // Remove the old treeview data if it exists  RemoveTreeNodeData(tvuCategories);  // add a parent node  aRootNode := ATreeVu.Items.Add(nil, AParentNode);  //  for intCount := 0 to Pred(intTotal) do  begin    //    MyTreeData := TTreeData.Create;    //    try      // Save the ID into the special object      // so it can be added to the treeview      MyTreeData.ID := intCount;      tvuCategories.Items.AddChildObject(aRootNode, jsObj.Items[intCount].AsString, MyTreeData);    finally      //MyTreeData.Free;      <----- Do NOT free the created objects here, else node data is lost    end;  end;  //  tvuCategories.Items.EndUpdate;  When I click on a node in the tree, I want to see the ID saved Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmCategories.tvuCategoriesClick(Sender: TObject);var  AData: TTreeData;  ANode: TTreeNode;begin  // get the user's selection  if tvuCategories.Selected <> nil then  begin    AData := TTreeData.Create;    try      ANode := tvuCategories.Selected;      ShowMessage(TTreeData(ANode.Data).ID);    <---- Works as long as object is not yet freed!    finally      AData.Free;    end;  end;end;  I remove treenodes and the data they contain using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmCategories.RemoveTreeNodeData(ATreeVu: TTreeView);var  intCount: integer;begin  // Clear the listview items as well as any data found in the nodes  if ATreeVu.Items.Count > 0 then  begin    for intCount := 0 to Pred(ATreeVu.Items.Count) do    begin      if Assigned(ATreeVu.Items.Item[intCount].Data) then      begin        ATreeVu.Items.Item[intCount].Data := nil;        //TTreeData(ATreeVu.Items[intCount].Data).Free;   <---- Does not seem to work!      end;    end;  end;  //  ATreeVu.Items.Clear;end;  All this works only if I do NOT free the created MyTreeData objects after populating the treeview, but this means I have to free them somewhere else and so far I've been unable to do so & when I exit the program, I have memory leaks. I was thinking of storing their addresses after they are created so that I can free them later. Is that really necessary? What am I missing? Thanks, JD ---------------------------------------------------------------------- balazsszekely · 2016-05-29 13:00 Try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  var  intCount: integer;  TreeData: TTreeData;begin  // Clear the listview items as well as any data found in the nodes  if ATreeVu.Items.Count > 0 then  begin    for intCount := ATreeVu.Items.Count - 1 downto 0 do  begin      if ATreeVu.Items.Item[intCount].Data <> nil then      begin        TreeData := TTreeData(ATreeVu.Items.Item[intCount].Data);        FreeAndNil(TreeData);        ATreeVu.Items.Item[intCount].Data := nil;      end;    end;  end;  ATreeVu.Items.Clear;end; ---------------------------------------------------------------------- JD · 2016-05-29 14:21 Thanks GetMem. It worked.  JD ---------------------------------------------------------------------- howardpc · 2016-05-29 14:30 I think the lines: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if ATreeVu.Items.Count > 0 thenand Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ATreeVu.Items.Item[intCount].Data := nil; are redundant and could be removed. ---------------------------------------------------------------------- satyendra.shukla · 2016-09-02 17:45 Sorry for replying in this thread. I'm using fpc 3.0 on 32 bit Lazarus 1.6. The issue is that I can copy the one TTreeView (lets say tvFromTreeview) to another TTreeView (tvToTreeview) Object. All nodes are visible but Data Pointers are not copied I believe. Whenever, I tried to access the tvToTreeview type     PGridRec = ^TGridRec;     TGridRec = record                    GridAvailable: Boolean;                    ChildAvailable: Boolean;                    GridIndex:integer;                    GridBlank:Boolean;                    GridName:String;                    GridTypeDesc:String;                    GridType:Integer;                    GridStatus:Integer; //Enabled/Disabled                    GridParentTabIndex:integer;                    GridParentTabName:String;              end;  var   GridRecPtr: PGridRec;  .... //Added the values in GridRecPtr    memoBreadCrumb.Lines.Add(PGridRec(tvFromTreeView.Selected.Data)^.Gr idParentTabName); //This works fine  in Memo and data is displayed .... //But when it is assigned to another tree then Nodes are displayed without any issues BUT Data pointers are not copied to memo .... allControls.tvToTreeView.Items.BeginUpdate;              allControls.tvToTreeView.Items.Assign(tvFromTreeView.Items); allControls.tvToTreeView.Items.EndUpdate;    memoBreadCrumb.Lines.Add(PGridRec(tvToTreeView.Selected.Data)^.Grid ParentTabName); //This gives the error Access Denied ....                    Can anyone share the working example..