[Solved] Exception geting Data stored at TreeNode lazarus / 31-LCL ---------------------------------------------------------------------- BlueIcaro · 2022-02-17 14:56 Hi, I got a exception when I trying to get back the info stored at treenode data property. Any idea, why? Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls; type   pElog = ^TElog;   TElog = record     Id: integer;     Ruta: string;   end; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     TreeView1: TTreeView;     procedure Button1Click(Sender: TObject);     procedure TreeView1Click(Sender: TObject);   private   public   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var   Datos: pElog;   Nodo: TTreeNode; begin   new(Datos);   Nodo := TreeView1.Items.Add(nil, 'Jorge');   Datos^.Id := 1111;   Datos^.Ruta := 'Ruta';   Nodo.Data := @Datos;   Dispose(Datos); end; procedure TForm1.TreeView1Click(Sender: TObject); var   T: TTreeNode;   Info: pElog;   st: String; begin   T := TreeView1.Selected;   if T = nil then   begin     exit;   end;   Info := pElog( T.Data);   st := info^.Ruta;   ShowMessage(St);   Dispose(Info); end; end. I store the data on buttonclick event, But I cant retirve when I cick on item. Thanks /BlueIcaro ---------------------------------------------------------------------- Thaddy · 2022-02-17 15:02 Because you call dispose()? Of course eventually you need to call dispose at some point, but only when the tree goes out of scope. ---------------------------------------------------------------------- BlueIcaro · 2022-02-17 15:06 Quote from: Thaddy on February 17, 2022, 04:02:58 pmBecause you call dispose()? Of course eventually you need to call dispose at some point, but only when the tree goes out of scope. Sorry I didn't say where I got the exception. here: Code: [Select]  st := info^.Ruta;    /BlueIcaro ---------------------------------------------------------------------- wp · 2022-02-17 15:42 Dispose the pointer to the node's data in the OnDeletion event of the TreeView. ---------------------------------------------------------------------- BlueIcaro · 2022-02-17 15:52 Hi, thank It's solved. I removed the Dispose sentence, I add to onDeletion Here is the code solved, Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls; type   pElog = ^TElog;   TElog = record     Id: integer;     Ruta: string;   end; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     TreeView1: TTreeView;     procedure Button1Click(Sender: TObject);     procedure TreeView1Click(Sender: TObject);     procedure TreeView1Deletion(Sender: TObject; Node: TTreeNode);   private   public   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var   Datos: pElog;   Nodo: TTreeNode; begin   new(Datos);   Nodo := TreeView1.Items.Add(nil, 'Jorge');   Datos^.Id := 1111;   Datos^.Ruta := 'Ruta';   Nodo.Data := Datos; end; procedure TForm1.TreeView1Click(Sender: TObject); var   T: TTreeNode;   Info: pElog;   st: String; begin   T := TreeView1.Selected;   if T = nil then   begin     exit;   end;   St := pElog(T.Data)^.Ruta;   ShowMessage(St); end; procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode); begin     Dispose(pElog(Node.Data)); end;           /BlueIcaro ---------------------------------------------------------------------- wp · 2022-02-17 16:07 BTW, you can combine creation of a node and adding a data field to one single step (each one of the TTreeNodes.Add* methods comes with a variant having the data pointer as last argument; the method name ends with "Object"; e.g. Add --> AddObject; AddChild --> AddChildObject, etc - https://lazarus- ccr.sourceforge.io/docs/lcl/comctrls/ttreenodes.html): 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 TForm1.Button1Click(Sender: TObject);var  Datos: pElog;begin  new(Datos);  Datos^.Id := 1111;  Datos^.Ruta := 'Ruta';  TreeView1.Items.AddObject(nil, 'Jorge', Datos);end;