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

@ -947,6 +947,18 @@ void GDScript::_save_orphaned_subclasses() {
}
GDScript::~GDScript() {
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->lock();
}
while (SelfList<GDScriptFunctionState> *E = pending_func_states.first()) {
E->self()->_clear_stack();
pending_func_states.remove(E);
}
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->unlock();
}
for (Map<StringName, GDScriptFunction *>::Element *E = member_functions.front(); E; E = E->next()) {
memdelete(E->get());
}
@ -1364,16 +1376,22 @@ GDScriptInstance::GDScriptInstance() {
}
GDScriptInstance::~GDScriptInstance() {
if (script.is_valid() && owner) {
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
GDScriptLanguage::singleton->lock->lock();
#endif
script->instances.erase(owner);
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
while (SelfList<GDScriptFunctionState> *E = pending_func_states.first()) {
E->self()->_clear_stack();
pending_func_states.remove(E);
}
if (script.is_valid() && owner) {
script->instances.erase(owner);
}
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
}
/************* SCRIPT LANGUAGE **************/