Skip to content

armgit / godot

Public Imported from GitHub
0

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.cpp
index 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_type
case 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_type
List<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.gd
new file mode 100644
index 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.out
new file mode 100644
index 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