Is there an event fired in a form when application closes

463 views Asked by At

My application spawns several independent forms. Once when created the application forgets about them. All action is handled in that form itself. When the application closes the form closes also, by the RTS i suppose. This is fine except that neither the OnClose nor the OnDestroy event are being fired, so memory leaks occur. I can administer which forms are present (as i do now) but actually the application must completely forget about these forms.

Is there a way to detect inside a form, being not the application main form, that the application is in the process of being closed?

1

There are 1 answers

1
David Heffernan On BEST ANSWER

The OnDestroy event certainly will fire if the form is destroyed. It's invoked from the form's destruction code. So the only conclusion is that your form is not being destroyed and so is leaked.

There are a couple of obvious ways to make sure that your independent forms are not leaked:

  1. When you create then, pass either Application or MainForm as the owner parameter of the form. When the owner is destroyed, it will also destroy everything it owns, including your forms.
  2. Explicitly destroy these forms from, for example, the main form destructor or the .dpr file after the call to Application.Run returns.

Option 1 is the most commonly used approach.