Form destroy (OnDestroy) never called when using custom styles? XE7

Viewed 344

Delphi XE7 - When using custom styles (Project, Options, appearance) OnDestroy never gets called. Using default-native-windows skin-theme, form destroy is called as expected, is this normal? If so, what are other alternatives besides OnClose?

Blank project, OnDestroy():

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ShowMessage('destroy called only when not using styles');
end;

Solution and advice:

When using styles (see @andreas advice): onDestroy() is not a good place to put code, since application termination will not wait for all code to finish, some code might be executed but there is a chance not all.

1 Answers

The OnDestroy event is called irrespective of whether styles are used or not, when the form is in the process of being destroyed. You can confirm it by putting a break point on the ShowMessage() line (works of course only when running under the debugger) or by adding a call to Beep() (assuming your sound system is OK).

When the main form is destroyed, the program starts to prepare for termination. The call to ShowMessage() makes a furious attempt to show the message box but the process is already going down. The message box can even be seen briefly as a flash, in the case of not succeeding in staying visible. It is close to a miracle that the message box shows up in some OS's under any conditions.

Anyway, The best place to show any messages at the end, is in either OnCloseQuery() or OnClose() events. The OnDestroy() event is only meant to clean up any resources aquired in the OnCreate() event.

Related