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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With