From cb708de99978a4728b05a13a45a85d3d189e891c Mon Sep 17 00:00:00 2001 From: "Andrii Doroshenko (Xrayez)" Date: Thu, 5 Aug 2021 20:59:04 +0300 Subject: [PATCH] Fix memory leaks when instantiating `Mixin` manually for testing --- core/script/mixin_script/mixin_script.cpp | 6 ++++++ doc/Mixin.xml | 2 +- tests/project/goost/core/script/test_mixin_script.gd | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/script/mixin_script/mixin_script.cpp b/core/script/mixin_script/mixin_script.cpp index 8072865c..e2fb13db 100644 --- a/core/script/mixin_script/mixin_script.cpp +++ b/core/script/mixin_script/mixin_script.cpp @@ -518,6 +518,12 @@ Variant Mixin::call(const StringName &p_method, const Variant **p_args, int p_ar if (real_owner) { return real_owner->call(p_method, p_args, p_argcount, r_error); } +#ifdef DEBUG_ENABLED + // The following allows to call `Mixin.free()`. + // This is not strictly needed because Mixin objects should not be instantiated directly, + // and doing so is discouraged, but this can make debug builds more robust for fuzz testing. + Object::call(p_method, p_args, p_argcount, r_error); +#endif return Variant(); } diff --git a/doc/Mixin.xml b/doc/Mixin.xml index ba0a5330..bc2e7ef6 100644 --- a/doc/Mixin.xml +++ b/doc/Mixin.xml @@ -5,7 +5,7 @@ A class which extends the run-time functionality of objects that use [MixinScript]. All scripts added to [MixinScript] must inherit from [Mixin]. - Mixin objects are instantiated internally and cannot be used as a [Node] in the scene tree. + [b]Warning:[/b] [Mixin] objects are instantiated internally and cannot be used as a [Node] in the scene tree. Do not instantiate [Mixin] objects manually. diff --git a/tests/project/goost/core/script/test_mixin_script.gd b/tests/project/goost/core/script/test_mixin_script.gd index b8fc2cdb..e6c7e0da 100644 --- a/tests/project/goost/core/script/test_mixin_script.gd +++ b/tests/project/goost/core/script/test_mixin_script.gd @@ -125,3 +125,11 @@ func test_move_script(): assert_eq(n.tell(), "First", "Did not move") ms.move_mixin(1, MixinFirst) assert_eq(n.tell(), "Second") + + +func test_mixin_get_owner(): + var m = Mixin.new() + var o = m.get_owner() + assert_null(o) + m.free() + assert_freed(m, "Freed")