[SOLVED] problem closing 2 dynamically created forms lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2017-10-28 09:23 My program has a problem and I don't see my mistake: my program can open 2 dynamically created forms (to show some informations) with the following rules:  - it is allowed that both forms are opened at the same time  - it is not allowed that the same form is opened more than once  - the forms should be opened non-modal  - when the main program terminates, both forms must be closed automatically 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 // This program makeks no sense. It's only an extract of a bigger// program and reduced to the minimum to demonstrate the problem. {$mode objfpc}{$H+}{$apptype console} unit Unit1; interface uses SysUtils, Forms, StdCtrls; {reduced to the minimum} type TForm1 = class(TForm)  Button1: TButton;  Button2: TButton;  procedure Button1Click(Sender: TObject);  procedure Button2Click(Sender: TObject);  procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); private { private declarations } public  { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} type                               {class for a dynamically created form: }   TFormX = class(TForm) {procedure for every close of the Form: }      procedure CloseX(Sender: TObject; var CloseAction: TCloseAction);    private { private declarations }      pStop: ^boolean; {control variable for to close the form}   public  { public declarations }   end; procedure TFormX.CloseX(Sender: TObject; var CloseAction: TCloseAction);   {called by event "OnClose" of the dynamically created forms}   begin   writeln('CloseX');   pStop^:=true; {stopps the waiting loop in show_Form()}   end; procedure show_Form(msg: string; x0: integer; var stop: boolean);   {creates a Form dynamically and waites for closing the Form}   var FormX: TFormX;   begin   FormX:=TFormX.CreateNew(nil);   FormX.pStop:=@stop; {control variable for to close the form}   FormX.OnClose:=@FormX.CloseX; {procedure for event "OnClose"}   FormX.Caption:=msg; {show something in the form...}    FormX.SetBounds(x0,300,150,150);   FormX.Show; {show the new form}    writeln('started waiting for close');   stop:=false;                        {wait for the closing of the form: }   repeat Application.ProcessMessages;          sleep(1); {avoid high CPU-load}   until  stop; {variable changed in CloseX()}   writeln('stopped waiting');    FreeAndNil(FormX);   end; {show_Form} var stop1,stop2: boolean; procedure TForm1.Button1Click(Sender: TObject);   const activ1: boolean = false;   begin   if activ1 then writeln('still running #1') else      begin      activ1:=true;      show_Form('hello-1', 300, stop1);      writeln('finished #1');      activ1:=false;      end;   end; procedure TForm1.Button2Click(Sender: TObject);   const activ2: boolean = false;   begin   if activ2 then writeln('still running #2') else      begin      activ2:=true;      show_Form('hello-2', 500, stop2);      writeln('finished #2');      activ2:=false;      end;   end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);   begin   writeln('Form1_Close');   stop1:=true; {stopps waiting loop in show_Form()}   stop2:=true; {stopps waiting loop in show_Form()}   end; end. When you do the following:  - click button 1 (opens form 1)  - close form 1  - click button 2 (opens form 2)  - close form 2  - click button 1 (opens form 1 again) everything is fine. The output shows:     started waiting for close     CloseX     stopped waiting     finished #1     started waiting for close     CloseX     stopped waiting     finished #2     started waiting for close But when you do the following:  - click button 1 (opens form 1)  - click button 2 (opens form 2)  - close form 1  - click button 1 (should open form 1 again) nothing happens. The output shows:     started waiting for close     started waiting for close     CloseX     still running #1 That means, closing of form #1 did not work correctly. After "CloseX" we should see "stopped waiting" and "finished #1", but this is not excecuted. Why? To control the closing of the 2 forms, 2 global variables "stop1" and "stop2" are used. They are submitted to each form as a parameter and they are set to True in FormClose() when the main program terminates. The benefit is: FormClose() must not know 'TFormX' and it works independent of the dynamically forms having ever been created or not. I'm using Lazarus 1.8.0 RC5 with FPC 3.0.4 but had the same results with Lazarus 1.6.2 and FPC 3.0.0. Both on Windows 7 32 bit. I attached a small project. Thanks a lot in advance. ---------------------------------------------------------------------- taazz · 2017-10-28 09:50 One main error, You call application.processmessages. Remove it. 1) The onclose event of a form has a parameter named CloseAction set it to caFree to inform the framework that you want the form to be destroyed. 2) the owner of a component, passed in the constructor, is responsible for destroying it. Pass the application as the owner for your forms, that will destroy them automatically, when the application ends. ---------------------------------------------------------------------- Hartmut · 2017-10-28 10:50 Thank you taazz for your reply. I removed Application.ProcessMessages from the loop, but then, when pressing button 1, form #1 is not drawn completely and the program hangs (see screenshot).  So I inserted ProcessMessages again to the loop. I attached "CloseAction:=caFree;" to both TFormX.CloseX() and TForm1.FormClose(), but then, when closing form #1, I get "Project project1 has raised Exception class 'External: SIGSEGV' at address 40c93f" and then an assembler window opens. You wrote "Pass the application as the owner for your forms". How do I do that? (I'm a beginner to the LCL, sorry). ---------------------------------------------------------------------- taazz · 2017-10-28 11:18 I mend to remove the loop after the show completely here is a quick demonstration of how this works. 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 // This program makeks no sense. It's only an extract of a bigger// program and reduced to the minimum to demonstrate the problem. {$mode objfpc}{$H+}{$apptype console} unit Unit1; interface uses SysUtils, Forms, StdCtrls; {reduced to the minimum} type TForm1 = class(TForm)  Button1: TButton;  Button2: TButton;  procedure Button1Click(Sender: TObject);  procedure Button2Click(Sender: TObject);  procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); private { private declarations } public  { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} type                               {class for a dynamically created form: }   TFormX = class(TForm) {procedure for every close of the Form: }      procedure CloseX(Sender: TObject; var CloseAction: TCloseAction);    public  { public declarations }   end; procedure TFormX.CloseX(Sender: TObject; var CloseAction: TCloseAction);   {called by event "OnClose" of the dynamically created forms}   begin   writeln('CloseX');   //pStop^:=true; {stopps the waiting loop in show_Form()}     CloseAction := caFree;   end; procedure show_Form(msg: string; x0: integer; var stop: boolean);   {creates a Form dynamically and waites for closing the Form}   var FormX: TFormX;   begin   FormX:=TFormX.CreateNew(Application);   FormX.OnClose:=@FormX.CloseX; {procedure for event "OnClose"}   FormX.Caption:=msg; {show something in the form...}    FormX.SetBounds(x0,300,150,150);   FormX.Show; {show the new form} end; {show_Form} var stop1,stop2: boolean; procedure TForm1.Button1Click(Sender: TObject);   const activ1: boolean = false;   begin   if activ1 then writeln('still running #1') else      begin      activ1:=true;      show_Form('hello-1', 300, stop1);      writeln('finished #1');      activ1:=false;      end;   end; procedure TForm1.Button2Click(Sender: TObject);   const activ2: boolean = false;   begin   if activ2 then writeln('still running #2') else      begin      activ2:=true;      show_Form('hello-2', 500, stop2);      writeln('finished #2');      activ2:=false;      end;   end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);   begin   writeln('Form1_Close');   end; end. The framework will make sure that the dynamic forms will be destroyed on exit. There is the small issue with identifying if a form is already open which can be solved in various ways eg. make actv1, activ2 fields of the form and pass them to the TformX's object to set to false on exit or search the active forms list for a TFormX based form and see if its parameters are the same with the ones you are asked to show bringing it to focus if you found one. ---------------------------------------------------------------------- Hartmut · 2017-10-28 12:01 The solution from taazz works fine. To solve the issue, that each form is not allowed to be opened more than once, I changed variables "activ1" and "activ2" to be global and to be reset in TFormX.CloseX(). See the final code below. Thanks a lot to taazz for your valuable help. There is just one question left for me to learn: why does TFormX.CloseX() contain the recommended "CloseAction := caFree" and TForm1.FormClose() not? 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 // This program makeks no sense. It's only an extract of a bigger// program and reduced to the minimum to demonstrate the problem. {$mode objfpc}{$H+}{$apptype console} unit Unit1; interface uses SysUtils, Forms, StdCtrls; {reduced to the minimum} type TForm1 = class(TForm)  Button1: TButton;  Button2: TButton;  procedure Button1Click(Sender: TObject);  procedure Button2Click(Sender: TObject);  procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); private { private declarations } public  { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} type                               {class for a dynamically created form: }   TFormX = class(TForm) {procedure for every close of the Form: }      procedure CloseX(Sender: TObject; var CloseAction: TCloseAction);    private { private declarations }      pActiv: ^boolean; {control variable to reset on closing the form}   public  { public declarations }   end; procedure TFormX.CloseX(Sender: TObject; var CloseAction: TCloseAction);   {called by event "OnClose" of the dynamically created forms}   begin   writeln('CloseX');   pActiv^:=false; {allows the form to be opened again}   CloseAction:=caFree;   end; procedure show_Form(msg: string; x0: integer; var activ: boolean);   {creates a Form dynamically}   var FormX: TFormX;   begin   FormX:=TFormX.CreateNew(Application);   FormX.pActiv:=@activ; {control variable to reset on closing the form}   FormX.OnClose:=@FormX.CloseX; {procedure for event "OnClose"}   FormX.Caption:=msg; {show something in the form...}    FormX.SetBounds(x0,300,150,150);   FormX.Show; {show the new form} end; const activ1: boolean = false;      activ2: boolean = false; procedure TForm1.Button1Click(Sender: TObject);   begin   if activ1 then writeln('still running #1') else      begin      activ1:=true;      show_Form('hello-1', 300, activ1);      end;   end; procedure TForm1.Button2Click(Sender: TObject);   begin   if activ2 then writeln('still running #2') else      begin      activ2:=true;      show_Form('hello-2', 500, activ2);      end;   end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);   begin   writeln('Form1_Close');   end; end. ---------------------------------------------------------------------- taazz · 2017-10-28 12:10 The framework defaults to CloseAction = caHide for all forms unless the form is the application's main form in which case it defaults to caFree. Less problems with autocreated forms and expectation that it is still in memory after close. ---------------------------------------------------------------------- munair · 2017-10-28 12:21 Another solution would be to use an array of forms. Here is a working example that unhides/hides a form when the button is clicked. A second unit is used for the custom form and array definition. Instantiation of this second form is commented out in the lpr file. Just an idea. unit1: 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 uFormMain; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, uFormX; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  if not FormX[0].Visible then    FormX[0].Show  else    FormX[0].Hide;end; procedure TForm1.Button2Click(Sender: TObject);begin  if not FormX[1].Visible then    FormX[1].Show  else  FormX[1].Hide;end; procedure TForm1.FormCreate(Sender: TObject);begin  SetLength(FormX, 1);  FormX[0] := TForm2.Create(Application);  FormX[1] := TForm2.Create(Application);end; end. unit2: 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 uFormX; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs; type   { TForm2 }   TForm2 = class(TForm)  private    { private declarations }  public    { public declarations }  end; var  FormX: array of TForm2; implementation {$R *.lfm} { TForm2 } end.  Note that both forms (hidden or shown) are destroyed when the application quits. ---------------------------------------------------------------------- Hartmut · 2017-10-28 12:53 The solution from Munair is another interesting approach, because the forms are always created and only switched from Show to Hide. Thanks a lot to taazz and Munair. This is a great forum! ---------------------------------------------------------------------- mai · 2017-10-28 13:19 better than bloody MX , anyway ...