Does an event occur in the form when the application closes

Several independent forms appear in my application. Once upon creation, the application forgets about them. All actions are processed in this form. When the application closes the form closes as well, by RTS, I suppose. This is fine, except that neither OnClose nor the OnDestroy event fires, so a memory leak occurs. I can administer which forms are present (as it is now), but in fact the application should completely forget about these forms.

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

+4
source share
1 answer

The OnDestroy event will certainly fire if the form is destroyed. It is called from the form destruction code. Thus, the only conclusion is that your form is not destroyed and therefore leaks out.

There are several obvious ways to make sure your independent forms are not leaked:

  • When you create, pass either Application or MainForm as the form owner parameter. When the owner is destroyed, he will also destroy everything that he has, including your forms.
  • Explicitly destroy these forms, for example, using the destructor of the main form or the .dpr file after returning the call to Application.Run .

Option 1 is the most commonly used approach.

+6
source

Source: https://habr.com/ru/post/1392877/


All Articles