Fix object leaks caused by unfulfilled yields

Now the stack saved in a `GDScriptFunctionState` is cleared as soon as the `yield()` operation is known not to be resumed because either the script, the instance or both are deleted.

This clears problems like leaked objects by eliminating cases of circular references between `GDScriptFunctionState`s preventing them and the objects they refer to in their saved stacks from being released. As an example, this makes using `SceneTreeTimer` safer.

Furthermore, with this change it's now possible to print early warnings about `yield()`s to released script/instances, as now we know they won't be successfully resumed as the condition for that happens. However, this PR doesn't add such messages, to keep the observed behavior the same for the time being.

Also, now a backup of the function name in `GDScriptFunctionState` is used, since the script may not be valid by the time the function name is needed for the resume-after-yield error messages.
This commit is contained in:
Pedro J. Estébanez 2020-05-05 17:49:17 +02:00
parent 8426ed2650
commit 64344e6d1d
5 changed files with 101 additions and 42 deletions

View file

@ -1274,12 +1274,24 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
gdfs->state.ip = ip + ipofs;
gdfs->state.line = line;
gdfs->state.script = _script;
gdfs->state.script_id = _script->get_instance_id();
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
_script->pending_func_states.add(&gdfs->scripts_list);
if (p_instance) {
gdfs->state.instance = p_instance;
p_instance->pending_func_states.add(&gdfs->instances_list);
} else {
gdfs->state.instance = NULL;
}
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
#ifdef DEBUG_ENABLED
gdfs->state.function_name = name;
gdfs->state.script_path = _script->get_path();
#endif
gdfs->state.instance = p_instance;
gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
//gdfs->state.result_pos=ip+ipofs-1;
gdfs->state.defarg = defarg;
gdfs->function = this;
@ -1832,16 +1844,16 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
return false;
if (p_extended_check) {
if (state.instance_id) {
// Class instance gone? (Otherwise script is valid for sure, because the instance has a ref to the script)
if (!ObjectDB::get_instance(state.instance_id)) {
return false;
}
} else {
// 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;
}
#ifndef NO_THREADS
MutexLock lock(GDScriptLanguage::get_singleton()->lock);
#endif
// Script gone?
if (!scripts_list.in_list()) {
return false;
}
// Class instance gone? (if not static function)
if (state.instance && !instances_list.in_list()) {
return false;
}
}
@ -1851,22 +1863,27 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
Variant GDScriptFunctionState::resume(const Variant &p_arg) {
ERR_FAIL_COND_V(!function, Variant());
if (state.instance_id) {
if (!ObjectDB::get_instance(state.instance_id)) {
{
#ifndef NO_THREADS
MutexLock lock(GDScriptLanguage::singleton->lock);
#endif
if (!scripts_list.in_list()) {
#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_path + ":" + itos(state.line));
ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after yield, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
#else
return Variant();
#endif
}
} else {
if (!ObjectDB::get_instance(state.script_id)) {
if (state.instance && !instances_list.in_list()) {
#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));
ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after yield, but class instance is gone. At script: " + state.script_path + ":" + itos(state.line));
#else
return Variant();
#endif
}
// Do these now to avoid locking again after the call
scripts_list.remove_from_list();
instances_list.remove_from_list();
}
state.result = p_arg;
@ -1889,6 +1906,8 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
state.result = Variant();
if (completed) {
_clear_stack();
if (first_state.is_valid()) {
first_state->emit_signal("completed", ret);
} else {
@ -1898,18 +1917,22 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
#ifdef DEBUG_ENABLED
if (ScriptDebugger::get_singleton())
GDScriptLanguage::get_singleton()->exit_function();
if (state.stack_size) {
//free stack
Variant *stack = (Variant *)state.stack.ptr();
for (int i = 0; i < state.stack_size; i++)
stack[i].~Variant();
}
#endif
}
return ret;
}
void GDScriptFunctionState::_clear_stack() {
if (state.stack_size) {
Variant *stack = (Variant *)state.stack.ptr();
for (int i = 0; i < state.stack_size; i++)
stack[i].~Variant();
state.stack_size = 0;
}
}
void GDScriptFunctionState::_bind_methods() {
ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
@ -1919,18 +1942,22 @@ void GDScriptFunctionState::_bind_methods() {
ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
}
GDScriptFunctionState::GDScriptFunctionState() {
GDScriptFunctionState::GDScriptFunctionState() :
scripts_list(this),
instances_list(this) {
function = NULL;
}
GDScriptFunctionState::~GDScriptFunctionState() {
if (function != NULL) {
//never called, deinitialize stack
for (int i = 0; i < state.stack_size; i++) {
Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
v->~Variant();
}
}
_clear_stack();
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
scripts_list.remove_from_list();
instances_list.remove_from_list();
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
}