Skip to content

armgit / godot

Public Imported from GitHub
0

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

GDScript: Fix shutdown crash when `ScriptInstance` references its owner

HolonProductiona9c94cd21fc1
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp
index 9fc63261f30..aaa745dd652 100644
--- a/modules/gdscript/gdscript.cpp
+++ b/modules/gdscript/gdscript.cpp
@@ -2272,7 +2272,13 @@ void GDScriptLanguage::finish() {
while (script->instances.first() != nullptr) {
// Turns instances into core objects that can outlive `GDScriptLanguage`.
const SelfList<GDScriptInstance> *elem = script->instances.first();
+
+ // In case the last reference to the owner comes from the script instance itself, we need to ensure that `set_script` can finish cleanly, before destructing the owner.
+ Variant owner = elem->self()->get_owner();
+ std::ignore = owner; // Suppress unused warnings, holding the reference is the point.
+
elem->self()->get_owner()->set_script(Variant());
+
ERR_BREAK_MSG(script->instances.first() == elem, "GDScript bug (please report): Detaching a script does not destruct instance.");
}
}