From cf04c46a5ee86d239f820004b721e2f2729d0abe Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Tue, 28 Dec 2021 15:29:21 +0800 Subject: [PATCH] Clear pending function states when reloading GDScript (cherry picked from commit 53af7ee48296d7e39547169574b8ec6f24ba38eb) --- modules/gdscript/gdscript.cpp | 21 +++++++++++++-------- modules/gdscript/gdscript.h | 1 + 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 3a7e668dfdd..4ef8d178915 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -77,6 +77,17 @@ Object *GDScriptNativeClass::instance() { return ClassDB::instance(name); } +void GDScript::_clear_pending_func_states() { + GDScriptLanguage::get_singleton()->lock.lock(); + while (SelfList *E = pending_func_states.first()) { + // 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(); + } + GDScriptLanguage::get_singleton()->lock.unlock(); +} + GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error) { /* STEP 1, CREATE */ @@ -597,6 +608,7 @@ Error GDScript::reload(bool p_keep_state) { for (Map>::Element *E = subclasses.front(); E; E = E->next()) { _set_subclass_path(E->get(), path); } + _clear_pending_func_states(); return OK; } @@ -923,14 +935,7 @@ void GDScript::_save_orphaned_subclasses() { } GDScript::~GDScript() { - GDScriptLanguage::get_singleton()->lock.lock(); - while (SelfList *E = pending_func_states.first()) { - // 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(); - } - GDScriptLanguage::get_singleton()->lock.unlock(); + _clear_pending_func_states(); for (Map::Element *E = member_functions.front(); E; E = E->next()) { memdelete(E->get()); diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 026e0c3455e..476d8abd579 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -112,6 +112,7 @@ class GDScript : public Script { SelfList script_list; SelfList::List pending_func_states; + void _clear_pending_func_states(); GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error);