summaryrefslogtreecommitdiffstats
path: root/include/godot_cpp
diff options
context:
space:
mode:
authorDavid Snopek <dsnopek@gmail.com>2024-05-07 12:54:35 -0500
committerDavid Snopek <dsnopek@gmail.com>2024-05-10 19:51:31 -0500
commit88df025aa0ab92ed176f91dd835c004fa26f0ed0 (patch)
tree61c4a3c454986d3723a095ff46f91f0f3eecafb0 /include/godot_cpp
parent85172dad1bd3cfed8cf8b4c2826f34cfe211af05 (diff)
downloadredot-cpp-88df025aa0ab92ed176f91dd835c004fa26f0ed0.tar.gz
Clean up instance bindings for engine singletons to prevent crash
Diffstat (limited to 'include/godot_cpp')
-rw-r--r--include/godot_cpp/core/class_db.hpp18
-rw-r--r--include/godot_cpp/godot.hpp1
2 files changed, 19 insertions, 0 deletions
diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp
index ca5a326..8ef7f8e 100644
--- a/include/godot_cpp/core/class_db.hpp
+++ b/include/godot_cpp/core/class_db.hpp
@@ -45,6 +45,7 @@
#include <godot_cpp/variant/callable_method_pointer.hpp>
#include <list>
+#include <mutex>
#include <set>
#include <string>
#include <unordered_map>
@@ -104,6 +105,8 @@ private:
static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
// Used to remember the custom class registration order.
static std::vector<StringName> class_register_order;
+ static std::unordered_map<StringName, Object *> engine_singletons;
+ static std::mutex engine_singletons_mutex;
static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
static void initialize_class(const ClassInfo &cl);
@@ -153,6 +156,21 @@ public:
instance_binding_callbacks[p_name] = p_callbacks;
}
+ static void _register_engine_singleton(const StringName &p_class_name, Object *p_singleton) {
+ std::lock_guard<std::mutex> lock(engine_singletons_mutex);
+ std::unordered_map<StringName, Object *>::const_iterator i = engine_singletons.find(p_class_name);
+ if (i != engine_singletons.end()) {
+ ERR_FAIL_COND((*i).second != p_singleton);
+ return;
+ }
+ engine_singletons[p_class_name] = p_singleton;
+ }
+
+ static void _unregister_engine_singleton(const StringName &p_class_name) {
+ std::lock_guard<std::mutex> lock(engine_singletons_mutex);
+ engine_singletons.erase(p_class_name);
+ }
+
template <typename N, typename M, typename... VarArgs>
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp
index 95a0387..822363d 100644
--- a/include/godot_cpp/godot.hpp
+++ b/include/godot_cpp/godot.hpp
@@ -162,6 +162,7 @@ extern "C" GDExtensionInterfaceObjectDestroy gdextension_interface_object_destro
extern "C" GDExtensionInterfaceGlobalGetSingleton gdextension_interface_global_get_singleton;
extern "C" GDExtensionInterfaceObjectGetInstanceBinding gdextension_interface_object_get_instance_binding;
extern "C" GDExtensionInterfaceObjectSetInstanceBinding gdextension_interface_object_set_instance_binding;
+extern "C" GDExtensionInterfaceObjectFreeInstanceBinding gdextension_interface_object_free_instance_binding;
extern "C" GDExtensionInterfaceObjectSetInstance gdextension_interface_object_set_instance;
extern "C" GDExtensionInterfaceObjectGetClassName gdextension_interface_object_get_class_name;
extern "C" GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to;