# TCastleView.Stop called from FINALIZATION section

**URL:** https://forum.castle-engine.io/t/tcastleview-stop-called-from-finalization-section/1526
**Category:** Uncategorized
**Created:** [February 8, 2025, 11:56am UTC](https://forum.castle-engine.io/t/tcastleview-stop-called-from-finalization-section/1526 "2025-02-08T11:56:46Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [May 12, 2025, 4:40pm UTC](https://forum.castle-engine.io/t/tcastleview-stop-called-from-finalization-section/1526/7 "2025-05-12T16:40:05Z")

</div>

> [@edj](#):
>
> Maybe if you used threads more, you would realize the danger of doing too much in finalization.

Doing things in `OnCloseQuery` is executing them in the same thread as you would as in `finalization`. I don’t think you achieve anything by doing things in `OnCloseQuery` versus in “`finalization` after making sure that views have stopped by `Application.MainWindow.Container.View := nil`”.

And since `OnCloseQuery` is _not_ going to run always when window is closed by explicit `Window.Close` ( [Where to intercept closing application? - #3 by michalis](https://forum.castle-engine.io/t/where-to-intercept-closing-application/1897/3) ), it’s still not the best place to make “cleanup that must be executed always” 🙂

I realize that things are difficult when using threads, and client/server, but this doesn’t change the above recommendation.

- If you have threads, you must cleanup the data correctly.

- If you client/server in threads, you may need to also wait for those threads.

- I realize it’s not easy (and why Indy can make either memory leaks or crashes, as documented in [3.4. Note: There are known memory leaks when using Indy](https://castle-engine.io/multi_player#_note_there_are_known_memory_leaks_when_using_indy)).

- Still, if you want to make a “cleanup that always executes”, doing it in `Stop` of the view is what I recommend. This is our equivalent of `OnBeforeClose` callback that you mention.

---

_[View the full topic](https://forum.castle-engine.io/t/tcastleview-stop-called-from-finalization-section/1526)._
