* * *

Author Topic: [SOLVED] Deallocate TTreeView  (Read 218 times)

tudi_x

  • Sr. Member
  • ****
  • Posts: 430
[SOLVED] Deallocate TTreeView
« on: January 05, 2018, 11:27:21 am »
hi All,
please advise how to deallocate a TTreeView.
as per attached i am having a memory leak for each node i add.
thank you

Code: Pascal  [Select]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     TreeView1: TTreeView;
  15.     procedure Button1Click(Sender: TObject);
  16.     procedure FormDestroy(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. var
  32.   treeNode: TTreeNode;
  33.  
  34. begin
  35.   treeNode := TTreeNode.Create(nil);
  36.   treeNode := TreeView1.Items.Add(nil, 'x');
  37. end;
  38.  
  39. procedure TForm1.FormDestroy(Sender: TObject);
  40. var
  41.   i: word;
  42.  
  43. begin
  44.   if TreeView1.Items.Count > 0 then
  45.   begin
  46.     for i := TreeView1.Items.Count - 1 downto 0 do
  47.     begin
  48.       if TreeView1.Items.Item[i].Data <> nil then
  49.       begin
  50.         TreeView1.Items.Item[i].Data:= nil;
  51.       end;
  52.     end;
  53.   end;
  54.  
  55.   TreeView1.Items.Clear;
  56. end;
  57.  
  58. end.
« Last Edit: January 05, 2018, 11:50:05 am by tudi_x »

taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: Deallocate TTreeView
« Reply #1 on: January 05, 2018, 11:32:23 am »
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   treeNode: TTreeNode;
  4.  
  5. begin
  6.   //treeNode := TTreeNode.Create(nil);        <--- memory leak
  7.   treeNode := TreeView1.Items.Add(nil, 'x'); //<--- the add method creates a node
  8. end;
  9.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 430
Re: Deallocate TTreeView
« Reply #2 on: January 05, 2018, 11:49:53 am »
thank you!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus