Make sure that NativeScript's destructor is called.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alireza49

Is there any way to make sure that c++ class’s destructor is certainly called in GDNative module? Regardless of the module being added in scene tree or not?

Can’t you rephrase your question?
Normally you add your class as node’s script, then ctor and init() is called.
When you free object then dtor are called.

sash-rc | 2020-10-20 18:42

Yes, your answer is correct for most of times, but maybe a c++ script is not meant to be added to any node and is used simply by making a new object using new() method. I know that in this case the developer is responsible for freeing that object, but what i actually was looking for was a way to find out that the application is closing (quiting or whatever) or a signal that shows this. Then in _init function of c++ script, it would be possible to connect that signal to the free() method and so in this case we can say for certain that the dtor will be called even if the developer forgets to call the free() method.

alireza49 | 2020-10-20 19:53

Well, still didn’t get your exact use-case, but there’s a godot_gdnative_terminate (alongside with godot_gdnative_init), so you can handle your custom deallocations there.

Personally, for similar purpose I use Autoload singleton Node, where I destroy my stuff not related to scene tree (in it’s dtor). Also, it looks it’s possible to hook to MainLoop::_finalize().

Handling quit requests — Godot Engine (stable) documentation in English

sash-rc | 2020-10-20 23:07