From 88df025aa0ab92ed176f91dd835c004fa26f0ed0 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Tue, 7 May 2024 12:54:35 -0500 Subject: Clean up instance bindings for engine singletons to prevent crash --- src/core/class_db.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/core') diff --git a/src/core/class_db.cpp b/src/core/class_db.cpp index acead8b..d9a8bf9 100644 --- a/src/core/class_db.cpp +++ b/src/core/class_db.cpp @@ -43,6 +43,8 @@ namespace godot { std::unordered_map ClassDB::classes; std::unordered_map ClassDB::instance_binding_callbacks; std::vector ClassDB::class_register_order; +std::unordered_map ClassDB::engine_singletons; +std::mutex ClassDB::engine_singletons_mutex; GDExtensionInitializationLevel ClassDB::current_level = GDEXTENSION_INITIALIZATION_CORE; MethodDefinition D_METHOD(StringName p_name) { @@ -419,6 +421,22 @@ void ClassDB::deinitialize(GDExtensionInitializationLevel p_level) { }); class_register_order.erase(it, class_register_order.end()); } + + if (p_level == GDEXTENSION_INITIALIZATION_CORE) { + // Make a new list of the singleton objects, since freeing the instance bindings will lead to + // elements getting removed from engine_singletons. + std::vector singleton_objects; + { + std::lock_guard lock(engine_singletons_mutex); + singleton_objects.reserve(engine_singletons.size()); + for (const std::pair &pair : engine_singletons) { + singleton_objects.push_back(pair.second); + } + } + for (std::vector::iterator i = singleton_objects.begin(); i != singleton_objects.end(); i++) { + internal::gdextension_interface_object_free_instance_binding((*i)->_owner, internal::token); + } + } } } // namespace godot -- cgit v1.2.3