Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Clear held objects from `GDType` to allow GDExtensions to properly deinit.

Lukas Tenbrink533c02dafb36
diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp
index 67d53938403..4566914ba02 100644
--- a/core/object/class_db.cpp
+++ b/core/object/class_db.cpp
@@ -2005,8 +2005,13 @@ static LocalVector<GDType *> gdtype_leaked_autorelease_pool;
void ClassDB::unregister_extension_class(const StringName &p_class) {
ClassInfo *c = classes.getptr(p_class);
ERR_FAIL_NULL_MSG(c, vformat("Class '%s' does not exist.", String(p_class)));
+
+ // Clear the object instances from the GDExtension, so there are no references back
+ // to it.
+ c->gdtype->clear_held_objects();
// Leak the GDType until exit so potential consumers don't have dangling pointers.
gdtype_leaked_autorelease_pool.push_back(c->gdtype);
+
classes.erase(p_class);
default_values_cached.erase(p_class);
default_values.erase(p_class);
diff --git a/core/object/gdtype.cpp b/core/object/gdtype.cpp
index 884c7e775e7..3e4c9cb1b44 100644
--- a/core/object/gdtype.cpp
+++ b/core/object/gdtype.cpp
@@ -90,6 +90,19 @@ void GDType::initialize() {
init_state = InitState::MUTABLE;
}
+void GDType::clear_held_objects() {
+ for (const KeyValue<StringName, Member> &kv : _self_members) {
+ if (kv.value.type == Member::Type::METHOD) {
+ const_cast<MethodBind *>(kv.value.payload.method)->set_default_arguments(Vector<Variant>());
+ }
+ }
+ for (const KeyValue<StringName, LocalVector<MethodBind *>> &kv : self_compatibility_method_map) {
+ for (MethodBind *method_bind : kv.value) {
+ method_bind->set_default_arguments(Vector<Variant>());
+ }
+ }
+}
+
void GDType::bind_integer_constant(const StringName &p_enum, const StringName &p_name, int64_t p_constant, bool p_is_bitfield) {
ERR_FAIL_COND(Thread::get_caller_id() != owning_thread_id);
ERR_FAIL_COND(init_state != InitState::MUTABLE);
diff --git a/core/object/gdtype.h b/core/object/gdtype.h
index 1a85764a7b0..a35341af450 100644
--- a/core/object/gdtype.h
+++ b/core/object/gdtype.h
@@ -147,6 +147,11 @@ public:
InitState get_init_state() const { return init_state; }
void initialize();
+ // Removes all held objects, which at the time of writing are only MethodBind defaults.
+ // The reason to do this is to allow a GDExtension to unload itself, and Objects held
+ // here might have references back to the extension.
+ void clear_held_objects();
+
const GDType *get_super_type() const { return super_type; }
const StringName &get_name() const { return name; }
const StringName &get_super_type_name() const {