Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the order of initialization and finalization determined?

I Always thought that the correct order of forms's life cycle is initialization -> Create -> OnCreate -> OnDestroy -> Destroy -> finalization which works for following code.

Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TUMain, UMain);
Application.Run;

However when i make a little change...

Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TUMain, UMain);
if 1 = 2 then
  Application.Run;

then TUMain order changes to initialization -> Create -> OnCreate -> finalization -> OnDestroy -> Destroy

So I wonder, how is exact order controlled or at least what caused the change in this case?

like image 712
Triber Avatar asked Oct 28 '25 11:10

Triber


1 Answers

If you never call Application.Run then auto-created forms (or any forms created with Application.CreateForm, for that matter) are freed in FinalizeUnits rather than when the main form closes.

Ordinarily Application.Run assigns ExitProc which calls DoneApplication - this enumerates windows owned by the applicaton and frees them before FinalizeUnits is called. If Application.Run is not called then there is no assigned ExitProc and the forms do not get freed until FinalizeUnits is called.

like image 113
J... Avatar answered Oct 30 '25 00:10

J...



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!