OnCloseQuery purpose is mostly to check can we close the window – in case we should e.g. ask the user “you have modified the file X, do you want to save? save and exit / don’t save and exit / cancel”.
So yes, OnCloseQuery
is called earlier (before the views are stopped and finalization of units is called) but that’s because, when this is called, it’s not really sure whether we will close the window. And as docs say,
This is not called when you explicitly close the window by calling the Close method.
So it’s not really a good place for “cleanup”. To do cleanup, the Stop
method of a view is indeed best, as you mentioned.
If Stop
happens too late for you (it is done from finalization
of CastleWindow
unit, which may happen after finalization of one of your own units) then, before the finalization
where you need to have the view stopped, do Application.MainWindow.Container.View := nil
. This will stop any pending views, executing their Stop
methods. So the Stop
of your views will be reliably really always done before the code you want.