Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Fix not releasing RefCounted temporary at end of statement
nieboczekb76603aa61b5
diff --git a/modules/gdscript/gdscript_byte_codegen.cpp b/modules/gdscript/gdscript_byte_codegen.cppindex 1b5da0eac11..28a133af76d 100644--- a/modules/gdscript/gdscript_byte_codegen.cpp+++ b/modules/gdscript/gdscript_byte_codegen.cpp@@ -109,7 +109,7 @@ uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_typecase Variant::PACKED_COLOR_ARRAY:case Variant::PACKED_VECTOR4_ARRAY:case Variant::VARIANT_MAX:- // Arrays, dictionaries, and objects are reference counted, so we don't use the pool for them.+ // Arrays, dictionaries, and objects are reference counted, so we use the same pool for them.temp_type = Variant::NIL;break;}@@ -121,7 +121,7 @@ uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_typeList<int> &pool = temporaries_pool[temp_type];if (pool.is_empty()) {- StackSlot new_temp(temp_type, p_type.can_contain_object());+ StackSlot new_temp(temp_type, temp_type == Variant::NIL);int idx = temporaries.size();pool.push_back(idx);temporaries.push_back(new_temp);diff --git a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gdnew file mode 100644index 00000000000..ba16acd1688--- /dev/null+++ b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd@@ -0,0 +1,19 @@+class TestRefCounted:+ extends RefCounted+ func _notification(what: int) -> void:+ if what == NOTIFICATION_PREDELETE:+ print(" TestRefCounted released")++func test() -> void:+ test1()+ test2()++func test1(_a: Array = []) -> void:+ print("test1 (untyped Array default)")+ var _count = TestRefCounted.new().get_reference_count()+ print(" end of statement")++func test2(_a: Array[String] = []) -> void:+ print("test2 (typed Array[String] default)")+ var _count = TestRefCounted.new().get_reference_count()+ print(" end of statement")diff --git a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.outnew file mode 100644index 00000000000..a242eab92e5--- /dev/null+++ b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out@@ -0,0 +1,7 @@+GDTEST_OK+test1 (untyped Array default)+ TestRefCounted released+ end of statement+test2 (typed Array[String] default)+ TestRefCounted released+ end of statement