浏览代码

Merge pull request #65910 from KoBeWi/gdsus

Cleanup function state connections when destroying instance
Rémi Verschelde 2 年之前
父节点
当前提交
604493eb6e
共有 3 个文件被更改,包括 22 次插入2 次删除
  1. 12 2
      modules/gdscript/gdscript.cpp
  2. 9 0
      modules/gdscript/gdscript_function.cpp
  3. 1 0
      modules/gdscript/gdscript_function.h

+ 12 - 2
modules/gdscript/gdscript.cpp

@@ -1496,7 +1496,12 @@ GDScript::~GDScript() {
 			// Order matters since clearing the stack may already cause
 			// the GDScriptFunctionState to be destroyed and thus removed from the list.
 			pending_func_states.remove(E);
-			E->self()->_clear_stack();
+			GDScriptFunctionState *state = E->self();
+			ObjectID state_id = state->get_instance_id();
+			state->_clear_connections();
+			if (ObjectDB::get_instance(state_id)) {
+				state->_clear_stack();
+			}
 		}
 	}
 
@@ -1920,7 +1925,12 @@ GDScriptInstance::~GDScriptInstance() {
 		// Order matters since clearing the stack may already cause
 		// the GDSCriptFunctionState to be destroyed and thus removed from the list.
 		pending_func_states.remove(E);
-		E->self()->_clear_stack();
+		GDScriptFunctionState *state = E->self();
+		ObjectID state_id = state->get_instance_id();
+		state->_clear_connections();
+		if (ObjectDB::get_instance(state_id)) {
+			state->_clear_stack();
+		}
 	}
 
 	if (script.is_valid() && owner) {

+ 9 - 0
modules/gdscript/gdscript_function.cpp

@@ -296,6 +296,15 @@ void GDScriptFunctionState::_clear_stack() {
 	}
 }
 
+void GDScriptFunctionState::_clear_connections() {
+	List<Object::Connection> conns;
+	get_signals_connected_to_this(&conns);
+
+	for (Object::Connection &c : conns) {
+		c.signal.disconnect(c.callable);
+	}
+}
+
 void GDScriptFunctionState::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
 	ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));

+ 1 - 0
modules/gdscript/gdscript_function.h

@@ -628,6 +628,7 @@ public:
 	Variant resume(const Variant &p_arg = Variant());
 
 	void _clear_stack();
+	void _clear_connections();
 
 	GDScriptFunctionState();
 	~GDScriptFunctionState();