Exception when using a control which is created dynamically lazarus / 31-LCL ---------------------------------------------------------------------- milvus1962 · 2019-10-06 15:12 Dear users, using lazarus 2.04 on win7 the execution of the following code leads to an exception. I try to create a panel dynamically, also a button onto the panel. Clicking the button should destroy the panel but it does not work.  When I create the control elements with the form designer everything works fine. This is the code for the dynamical variant: 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 unit Unit1; {$mode objfpc}{$H+} interface uses        Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls; type        TForm1 = class(TForm)                Button1: TButton;      ButtonNew: TButton;   // created dynamically when clicking button1      PanelNew: TPanel;     // created dynamically when clicking button1              procedure Button1Click(Sender: TObject);      procedure ButtonNewClick(Sender: TObject);        private        public        end; var        Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);var   button: TButton;begin   PanelNew:= TPanel.Create(self);   with PanelNew do begin        Parent:= self;      Left:= 40;      Top:= 100;      Width:= 260;      Height:= 170;        end;   ButtonNew:= TButton.Create(PanelNew);   with ButtonNew do begin      Parent:= PanelNew;      Caption:= 'Close';      Left:= 100;      Top:= 100;      onClick:= @ButtonNewClick;        end;end; procedure TForm1.ButtonNewClick(Sender: TObject);begin   PanelNew.Destroy;end; end.  What is wrong? I read some tutorials with respect to this topic but did not find the error. Thank you, Horst ---------------------------------------------------------------------- Thaddy · 2019-10-06 15:19 I would not call destroy but free - or better release. ---------------------------------------------------------------------- jamie · 2019-10-06 16:01 The problem is obvious... you freed the control while still in a OnClick Event, the event is still active within the control when the Event exits.  You need to free the control somewhere else that isn't part of the object you are freeing.. ---------------------------------------------------------------------- marcov · 2019-10-06 17:27 Moreover you destroy the component, while you gave a different owner (the form, by passing SELF) ownership. It is not your component to destroy. ---------------------------------------------------------------------- milvus1962 · 2019-10-06 18:23 Ok, thank you. To use the method "Free" or "Release" instead of "Destroy" seems not to be the solution. It does not change anything. I understand the hint that the ButtonClick-event belongs to the button resp. to the panel which I want to unload and that this may cause a conflict. On the other hand when I create all the components with the lazarus form designer every works well. So it must be possible to reproduce this by native coding!? Sorry, but I do not understand the advice with respect to the owner. I thougt that the "create" method needs the parent as argument. What is the right owner? thanks to all, Horst ---------------------------------------------------------------------- avk · 2019-10-06 19:13 It seems you can free the component from its own handler like this (not tested): 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    ButtonNew: TButton;   // created dynamically when clicking button1    PanelNew: TPanel;     // created dynamically when clicking button1    procedure Button1Click(Sender: TObject);    procedure ButtonNewClick(Sender: TObject);  private    procedure AsyncFree(aData: PtrInt);  public  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin  PanelNew := TPanel.Create(self);  with PanelNew do    begin      Parent:= self;    Left := 40;      Top := 100;      Width := 260;      Height := 170;    end;  ButtonNew := TButton.Create(PanelNew);  with ButtonNew do    begin      Parent:= PanelNew;      Caption := 'Close';      Left := 100;      Top := 100;      onClick := @ButtonNewClick;    end;end; procedure TForm1.ButtonNewClick(Sender: TObject);begin  Application.QueueAsyncCall(@AsyncFree, 0);end; procedure TForm1.AsyncFree(aData: PtrInt);begin  PanelNew.Free;end; end. ---------------------------------------------------------------------- milvus1962 · 2019-10-06 19:25 This works! Great. Thanks.