From f17983e9cf03771001643d6343d0cc56881ec069 Mon Sep 17 00:00:00 2001 From: Yasha Borevich Date: Thu, 28 Jun 2018 18:40:11 +0300 Subject: [PATCH] Fix memory leak in GDScript during infinnity loops with yields (cherry picked from commit 30317296af1124e2092d3acd374ca0246afd5536) --- modules/gdscript/gdscript_function.cpp | 20 ++++++++++---------- modules/gdscript/gdscript_function.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp index 8c117a94fa0..70d3ae3c544 100644 --- a/modules/gdscript/gdscript_function.cpp +++ b/modules/gdscript/gdscript_function.cpp @@ -1537,7 +1537,7 @@ Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_ar GDScriptFunctionState *gdfs = Object::cast_to(ret); if (gdfs && gdfs->function == function) { completed = false; - gdfs->previous_state = Ref(this); + gdfs->first_state = first_state.is_valid() ? first_state : Ref(this); } } @@ -1545,10 +1545,10 @@ Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_ar state.result = Variant(); if (completed) { - GDScriptFunctionState *state = this; - while (state != NULL) { - state->emit_signal("completed", ret); - state = *(state->previous_state); + if (first_state.is_valid()) { + first_state->emit_signal("completed", ret); + } else { + emit_signal("completed", ret); } } @@ -1599,7 +1599,7 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) { GDScriptFunctionState *gdfs = Object::cast_to(ret); if (gdfs && gdfs->function == function) { completed = false; - gdfs->previous_state = Ref(this); + gdfs->first_state = first_state.is_valid() ? first_state : Ref(this); } } @@ -1607,10 +1607,10 @@ Variant GDScriptFunctionState::resume(const Variant &p_arg) { state.result = Variant(); if (completed) { - GDScriptFunctionState *state = this; - while (state != NULL) { - state->emit_signal("completed", ret); - state = *(state->previous_state); + if (first_state.is_valid()) { + first_state->emit_signal("completed", ret); + } else { + emit_signal("completed", ret); } } diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h index a04054cb5e3..cfcf8805250 100644 --- a/modules/gdscript/gdscript_function.h +++ b/modules/gdscript/gdscript_function.h @@ -234,7 +234,7 @@ class GDScriptFunctionState : public Reference { GDScriptFunction *function; GDScriptFunction::CallState state; Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error); - Ref previous_state; + Ref first_state; protected: static void _bind_methods();