Jump to content
PhilPlus

Action after all form destroy in project

Recommended Posts

Hello, in a VCL project I need to run some code when closing the application, after destroying all forms.

In the next sample I try to free a tstringlist but the line 'tsTest.Free;' is executed before the Tform onDestroy 

program Project1;
uses
  Vcl.Forms, System.Classes,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas'; // global contains var : tsTest : TStringList;
{$R *.res}
begin
  tsTest := TStringList.Create;
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
  tsTest.Free;
end.

It is just a simple example of my problem, any idea how to do it ?

Share this post


Link to post

A more or less easy solution would be to add the destructor call to the finalization section of Unit3:

....
initialization
  tsTest := TStringList.Create; // or wherever it must be created
finalization
  tsTest.Free
end.

And make sure that Unit3 is added to the project file before any form units:

program Project1;

uses
  VCL.Forms,
  Unit3 in 'Unit3.pas',
  Unit1 in 'Unit1.pas', {Form1}
....

Not guaranteed but will probably work.

 

Share this post


Link to post
3 hours ago, Alexander Elagin said:

A more or less easy solution would be to add the destructor call to the finalization section of Unit3:


....
initialization
  tsTest := TStringList.Create; // or wherever it must be created
finalization
  tsTest.Free
end.

And make sure that Unit3 is added to the project file before any form units:


program Project1;

uses
  VCL.Forms,
  Unit3 in 'Unit3.pas',
  Unit1 in 'Unit1.pas', {Form1}
....

Not guaranteed but will probably work.

 

Good idea, we can be sure that Unit3 initialization is used before other, but it is less clear for finalization.

 

Share this post


Link to post

Finalization sections are executed in the reverse order to initialization, so the first unit to initialize will be the last one to finalize.

From the documentation:

The Finalization Section

The finalization section is optional and can appear only in units that have an initialization section. The finalization section begins with the reserved word finalization and continues until the end of the unit. It contains statements that are executed when the main program terminates (unless the Halt procedure is used to terminate the program). Use the finalization section to free resources that are allocated in the initialization section.

Finalization sections are executed in the opposite order from initialization sections. For example, if your application initializes units A, B, and C, in that order, it will finalize them in the order C, B, and A.

Once a unit's initialization code starts to execute, the corresponding finalization section is guaranteed to execute when the application shuts down. The finalization section must therefore be able to handle incompletely initialized data, since, if a runtime error occurs, the initialization code might not execute completely.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×