Merge pull request #38288 from RandomShaper/imvu/fix_not_freed_gdsfuncstate

Fix leaked objects when game ends with yields in progress
This commit is contained in:
Rémi Verschelde 2020-04-29 17:30:28 +02:00 committed by GitHub
commit 92d4a0cbd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 11 deletions

View file

@ -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

View file

@ -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;