GDExtension: Prevent crash during shutdown as singletons are deleted

This commit is contained in:
David Snopek 2024-05-10 12:07:21 -05:00
parent 2ba22d1554
commit 14506a4282
4 changed files with 17 additions and 4 deletions

View File

@ -384,6 +384,12 @@ Engine::Engine() {
singleton = this; singleton = this;
} }
Engine::~Engine() {
if (singleton == this) {
singleton = nullptr;
}
}
Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) : Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) :
name(p_name), name(p_name),
ptr(p_ptr), ptr(p_ptr),

View File

@ -187,7 +187,7 @@ public:
bool notify_frame_server_synced(); bool notify_frame_server_synced();
Engine(); Engine();
virtual ~Engine() {} virtual ~Engine();
}; };
#endif // ENGINE_H #endif // ENGINE_H

View File

@ -295,6 +295,9 @@ GDExtensionManager::GDExtensionManager() {
} }
GDExtensionManager::~GDExtensionManager() { GDExtensionManager::~GDExtensionManager() {
if (singleton == this) {
singleton = nullptr;
}
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
GDExtensionCompatHashes::finalize(); GDExtensionCompatHashes::finalize();
#endif #endif

View File

@ -2095,9 +2095,13 @@ Object::~Object() {
_extension_instance = nullptr; _extension_instance = nullptr;
} }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
else if (_instance_bindings != nullptr && Engine::get_singleton()->is_extension_reloading_enabled()) { else if (_instance_bindings != nullptr) {
for (uint32_t i = 0; i < _instance_binding_count; i++) { Engine *engine = Engine::get_singleton();
GDExtensionManager::get_singleton()->untrack_instance_binding(_instance_bindings[i].token, this); GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) {
for (uint32_t i = 0; i < _instance_binding_count; i++) {
gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this);
}
} }
} }
#endif #endif