Merge pull request #38482 from RandomShaper/improve_yield_3.2

Fix object leaks caused by unfulfilled yields (3.2)
This commit is contained in:
Rémi Verschelde 2020-05-09 22:03:00 +02:00 committed by GitHub
commit aa57bb0473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 42 deletions

View File

@ -120,6 +120,9 @@ private:
public: public:
_FORCE_INLINE_ bool in_list() const { return _root; } _FORCE_INLINE_ bool in_list() const { return _root; }
_FORCE_INLINE_ void remove_from_list() {
if (_root) _root->remove(this);
}
_FORCE_INLINE_ SelfList<T> *next() { return _next; } _FORCE_INLINE_ SelfList<T> *next() { return _next; }
_FORCE_INLINE_ SelfList<T> *prev() { return _prev; } _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
_FORCE_INLINE_ const SelfList<T> *next() const { return _next; } _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }

View File

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

View File

@ -114,6 +114,8 @@ class GDScript : public Script {
String fully_qualified_name; String fully_qualified_name;
SelfList<GDScript> script_list; SelfList<GDScript> script_list;
SelfList<GDScriptFunctionState>::List pending_func_states;
GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error); GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error);
void _set_subclass_path(Ref<GDScript> &p_sc, const String &p_path); void _set_subclass_path(Ref<GDScript> &p_sc, const String &p_path);
@ -235,6 +237,8 @@ class GDScriptInstance : public ScriptInstance {
Vector<Variant> members; Vector<Variant> members;
bool base_ref; bool base_ref;
SelfList<GDScriptFunctionState>::List pending_func_states;
void _ml_call_reversed(GDScript *sptr, const StringName &p_method, const Variant **p_args, int p_argcount); void _ml_call_reversed(GDScript *sptr, const StringName &p_method, const Variant **p_args, int p_argcount);
public: public:
@ -319,6 +323,8 @@ struct GDScriptWarning {
class GDScriptLanguage : public ScriptLanguage { class GDScriptLanguage : public ScriptLanguage {
friend class GDScriptFunctionState;
static GDScriptLanguage *singleton; static GDScriptLanguage *singleton;
Variant *_global_array; Variant *_global_array;

View File

@ -1274,12 +1274,24 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
gdfs->state.ip = ip + ipofs; gdfs->state.ip = ip + ipofs;
gdfs->state.line = line; gdfs->state.line = line;
gdfs->state.script = _script; 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 #ifdef DEBUG_ENABLED
gdfs->state.function_name = name;
gdfs->state.script_path = _script->get_path(); gdfs->state.script_path = _script->get_path();
#endif #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.result_pos=ip+ipofs-1;
gdfs->state.defarg = defarg; gdfs->state.defarg = defarg;
gdfs->function = this; gdfs->function = this;
@ -1832,18 +1844,18 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
return false; return false;
if (p_extended_check) { if (p_extended_check) {
if (state.instance_id) { #ifndef NO_THREADS
// Class instance gone? (Otherwise script is valid for sure, because the instance has a ref to the script) MutexLock lock(GDScriptLanguage::get_singleton()->lock);
if (!ObjectDB::get_instance(state.instance_id)) { #endif
// Script gone?
if (!scripts_list.in_list()) {
return false; return false;
} }
} else { // Class instance gone? (if not static function)
// Script gone? (Static method, so there's no instance whose ref to the script can ensure it's valid) if (state.instance && !instances_list.in_list()) {
if (!ObjectDB::get_instance(state.script_id)) {
return false; return false;
} }
} }
}
return true; return true;
} }
@ -1851,22 +1863,27 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
Variant GDScriptFunctionState::resume(const Variant &p_arg) { Variant GDScriptFunctionState::resume(const Variant &p_arg) {
ERR_FAIL_COND_V(!function, Variant()); 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 #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 #else
return Variant(); return Variant();
#endif #endif
} }
} else { if (state.instance && !instances_list.in_list()) {
if (!ObjectDB::get_instance(state.script_id)) {
#ifdef DEBUG_ENABLED #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 #else
return Variant(); return Variant();
#endif #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; state.result = p_arg;
@ -1889,6 +1906,8 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
state.result = Variant(); state.result = Variant();
if (completed) { if (completed) {
_clear_stack();
if (first_state.is_valid()) { if (first_state.is_valid()) {
first_state->emit_signal("completed", ret); first_state->emit_signal("completed", ret);
} else { } else {
@ -1898,18 +1917,22 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (ScriptDebugger::get_singleton()) if (ScriptDebugger::get_singleton())
GDScriptLanguage::get_singleton()->exit_function(); 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 #endif
} }
return ret; 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() { void GDScriptFunctionState::_bind_methods() {
ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant())); 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))); 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; function = NULL;
} }
GDScriptFunctionState::~GDScriptFunctionState() { GDScriptFunctionState::~GDScriptFunctionState() {
if (function != NULL) { _clear_stack();
//never called, deinitialize stack #ifndef NO_THREADS
for (int i = 0; i < state.stack_size; i++) { GDScriptLanguage::singleton->lock->lock();
Variant *v = (Variant *)&state.stack[sizeof(Variant) * i]; #endif
v->~Variant(); scripts_list.remove_from_list();
} instances_list.remove_from_list();
} #ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
} }

View File

@ -294,12 +294,11 @@ public:
struct CallState { struct CallState {
GDScript *script; GDScript *script;
ObjectID script_id; GDScriptInstance *instance;
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
StringName function_name;
String script_path; String script_path;
#endif #endif
GDScriptInstance *instance;
ObjectID instance_id;
Vector<uint8_t> stack; Vector<uint8_t> stack;
int stack_size; int stack_size;
Variant self; Variant self;
@ -359,12 +358,18 @@ class GDScriptFunctionState : public Reference {
Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error); Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
Ref<GDScriptFunctionState> first_state; Ref<GDScriptFunctionState> first_state;
SelfList<GDScriptFunctionState> scripts_list;
SelfList<GDScriptFunctionState> instances_list;
protected: protected:
static void _bind_methods(); static void _bind_methods();
public: public:
bool is_valid(bool p_extended_check = false) const; bool is_valid(bool p_extended_check = false) const;
Variant resume(const Variant &p_arg = Variant()); Variant resume(const Variant &p_arg = Variant());
void _clear_stack();
GDScriptFunctionState(); GDScriptFunctionState();
~GDScriptFunctionState(); ~GDScriptFunctionState();
}; };