Merge pull request #38481 from RandomShaper/improve_yield
Fix object leaks caused by unfulfilled yields
This commit is contained in:
commit
a4b829410c
@ -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; }
|
||||||
|
@ -1052,6 +1052,16 @@ void GDScript::_init_rpc_methods_properties() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GDScript::~GDScript() {
|
GDScript::~GDScript() {
|
||||||
|
|
||||||
|
{
|
||||||
|
MutexLock lock(GDScriptLanguage::get_singleton()->lock);
|
||||||
|
|
||||||
|
while (SelfList<GDScriptFunctionState> *E = pending_func_states.first()) {
|
||||||
|
E->self()->_clear_stack();
|
||||||
|
pending_func_states.remove(E);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
@ -1470,9 +1480,15 @@ GDScriptInstance::GDScriptInstance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GDScriptInstance::~GDScriptInstance() {
|
GDScriptInstance::~GDScriptInstance() {
|
||||||
if (script.is_valid() && owner) {
|
|
||||||
MutexLock lock(GDScriptLanguage::singleton->lock);
|
|
||||||
|
|
||||||
|
MutexLock lock(GDScriptLanguage::get_singleton()->lock);
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,6 +117,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, Callable::CallError &r_error);
|
GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Callable::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);
|
||||||
@ -254,6 +256,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:
|
||||||
@ -347,6 +351,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;
|
||||||
|
@ -294,8 +294,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
|
|||||||
line = p_state->line;
|
line = p_state->line;
|
||||||
ip = p_state->ip;
|
ip = p_state->ip;
|
||||||
alloca_size = p_state->stack.size();
|
alloca_size = p_state->stack.size();
|
||||||
script = static_cast<GDScript *>(ObjectDB::get_instance(p_state->script_id));
|
script = p_state->script;
|
||||||
p_instance = p_state->instance_id.is_valid() ? static_cast<GDScriptInstance *>(ObjectDB::get_instance(p_state->instance_id)->get_script_instance()) : nullptr;
|
p_instance = p_state->instance;
|
||||||
defarg = p_state->defarg;
|
defarg = p_state->defarg;
|
||||||
self = p_state->self;
|
self = p_state->self;
|
||||||
|
|
||||||
@ -1281,11 +1281,21 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
|
|||||||
gdfs->state.alloca_size = alloca_size;
|
gdfs->state.alloca_size = alloca_size;
|
||||||
gdfs->state.ip = ip + ipofs;
|
gdfs->state.ip = ip + ipofs;
|
||||||
gdfs->state.line = line;
|
gdfs->state.line = line;
|
||||||
gdfs->state.script_id = script->get_instance_id();
|
gdfs->state.script = _script;
|
||||||
|
{
|
||||||
|
MutexLock lock(GDScriptLanguage::get_singleton()->lock);
|
||||||
|
_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
#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_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : ObjectID();
|
|
||||||
gdfs->state.defarg = defarg;
|
gdfs->state.defarg = defarg;
|
||||||
gdfs->function = this;
|
gdfs->function = this;
|
||||||
|
|
||||||
@ -1833,12 +1843,14 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (p_extended_check) {
|
if (p_extended_check) {
|
||||||
// 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 (state.instance_id.is_valid() && !ObjectDB::get_instance(state.instance_id)) {
|
|
||||||
|
// Script gone?
|
||||||
|
if (!scripts_list.in_list()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Script gone? (Static method, so there's no instance whose ref to the script can ensure it's valid)
|
// Class instance gone? (if not static function)
|
||||||
if (!ObjectDB::get_instance(state.script_id)) {
|
if (state.instance && !instances_list.in_list()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1849,20 +1861,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.is_valid() && !ObjectDB::get_instance(state.instance_id)) {
|
{
|
||||||
|
MutexLock lock(GDScriptLanguage::singleton->lock);
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
if (!ObjectDB::get_instance(state.script_id)) {
|
if (state.instance && !instances_list.in_list()) {
|
||||||
#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;
|
||||||
Callable::CallError err;
|
Callable::CallError err;
|
||||||
@ -1884,6 +1903,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 {
|
||||||
@ -1893,18 +1914,22 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) {
|
|||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
if (EngineDebugger::is_active())
|
if (EngineDebugger::is_active())
|
||||||
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()));
|
||||||
@ -1914,18 +1939,20 @@ 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 = nullptr;
|
function = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
GDScriptFunctionState::~GDScriptFunctionState() {
|
GDScriptFunctionState::~GDScriptFunctionState() {
|
||||||
|
|
||||||
if (function != nullptr) {
|
_clear_stack();
|
||||||
//never called, deinitialize stack
|
|
||||||
for (int i = 0; i < state.stack_size; i++) {
|
{
|
||||||
Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
|
MutexLock lock(GDScriptLanguage::singleton->lock);
|
||||||
v->~Variant();
|
scripts_list.remove_from_list();
|
||||||
}
|
instances_list.remove_from_list();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -293,11 +293,12 @@ private:
|
|||||||
public:
|
public:
|
||||||
struct CallState {
|
struct CallState {
|
||||||
|
|
||||||
ObjectID script_id;
|
GDScript *script;
|
||||||
|
GDScriptInstance *instance;
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
|
StringName function_name;
|
||||||
String script_path;
|
String script_path;
|
||||||
#endif
|
#endif
|
||||||
ObjectID instance_id;
|
|
||||||
Vector<uint8_t> stack;
|
Vector<uint8_t> stack;
|
||||||
int stack_size;
|
int stack_size;
|
||||||
Variant self;
|
Variant self;
|
||||||
@ -357,12 +358,18 @@ class GDScriptFunctionState : public Reference {
|
|||||||
Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
|
Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::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();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user