summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPedro J. Estébanez <pedrojrulez@gmail.com>2020-04-29 13:38:00 +0200
committerPedro J. Estébanez <pedrojrulez@gmail.com>2020-04-29 13:55:10 +0200
commitc427334393837b26f76e621f25c7886c5aa62336 (patch)
tree83ea91a920692e0ff6acc98e98fa261856e457d5
parentb3da42942301e621b0fe64474dbbe0b39cef336b (diff)
Fix leaked objects when game ends with yields in progress
-rw-r--r--modules/gdscript/gdscript_function.cpp30
-rw-r--r--modules/gdscript/gdscript_function.h6
2 files changed, 25 insertions, 11 deletions
diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp
index 4e0891921e..44640411bb 100644
--- a/modules/gdscript/gdscript_function.cpp
+++ b/modules/gdscript/gdscript_function.cpp
@@ -294,11 +294,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
line = p_state->line;
ip = p_state->ip;
alloca_size = p_state->stack.size();
- script = p_state->script.ptr();
- p_instance = p_state->instance;
+ script = static_cast<GDScript *>(ObjectDB::get_instance(p_state->script_id));
+ p_instance = p_state->instance_id.is_valid() ? static_cast<GDScriptInstance *>(ObjectDB::get_instance(p_state->instance_id)->get_script_instance()) : nullptr;
defarg = p_state->defarg;
self = p_state->self;
- //stack[p_state->result_pos]=p_state->result; //assign stack with result
} else {
@@ -1280,13 +1279,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
gdfs->state.stack_size = _stack_size;
gdfs->state.self = self;
gdfs->state.alloca_size = alloca_size;
- gdfs->state.script = Ref<GDScript>(_script);
gdfs->state.ip = ip + ipofs;
gdfs->state.line = line;
+ gdfs->state.script_id = script->get_instance_id();
+#ifdef DEBUG_ENABLED
+ gdfs->state.script_path = _script->get_path();
+#endif
gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : ObjectID();
- //gdfs->state.result_pos=ip+ipofs-1;
gdfs->state.defarg = defarg;
- gdfs->state.instance = p_instance;
gdfs->function = this;
retvalue = gdfs;
@@ -1833,9 +1833,14 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
return false;
if (p_extended_check) {
- //class instance gone?
- if (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id))
+ // Class instance gone? (Otherwise script is valid for sure, because the instance has a ref to the script)
+ if (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id)) {
return false;
+ }
+ // Script gone? (Static method, so there's no instance whose ref to the script can ensure it's valid)
+ if (!ObjectDB::get_instance(state.script_id)) {
+ return false;
+ }
}
return true;
@@ -1846,7 +1851,14 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
ERR_FAIL_COND_V(!function, Variant());
if (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id)) {
#ifdef DEBUG_ENABLED
- ERR_FAIL_V_MSG(Variant(), "Resumed function '" + String(function->get_name()) + "()' after yield, but class instance is gone. At script: " + state.script->get_path() + ":" + itos(state.line));
+ ERR_FAIL_V_MSG(Variant(), "Resumed function '" + String(function->get_name()) + "()' after yield, but class instance is gone. At script: " + state.script_path + ":" + itos(state.line));
+#else
+ return Variant();
+#endif
+ }
+ if (!ObjectDB::get_instance(state.script_id)) {
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_V_MSG(Variant(), "Resumed function '" + String(function->get_name()) + "()' after yield, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
#else
return Variant();
#endif
diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h
index acfc0a95b4..9d8e23d994 100644
--- a/modules/gdscript/gdscript_function.h
+++ b/modules/gdscript/gdscript_function.h
@@ -293,13 +293,15 @@ private:
public:
struct CallState {
+ ObjectID script_id;
+#ifdef DEBUG_ENABLED
+ String script_path;
+#endif
ObjectID instance_id;
- GDScriptInstance *instance;
Vector<uint8_t> stack;
int stack_size;
Variant self;
uint32_t alloca_size;
- Ref<GDScript> script;
int ip;
int line;
int defarg;