From c5f7a89b0f557c9540fa2c6af2cbb3a0fb5096a3 Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Fri, 24 Feb 2023 22:58:00 +0200 Subject: [PATCH] Core: Identity compare objects by id, not by pointers --- core/variant/variant.cpp | 2 +- tests/core/variant/test_variant.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 04e1561a0c4..2da95598739 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -3499,7 +3499,7 @@ bool Variant::identity_compare(const Variant &p_variant) const { switch (type) { case OBJECT: { - return _get_obj().obj == p_variant._get_obj().obj; + return _get_obj().id == p_variant._get_obj().id; } break; case DICTIONARY: { diff --git a/tests/core/variant/test_variant.h b/tests/core/variant/test_variant.h index dfbaa36af8f..024fcf53c43 100644 --- a/tests/core/variant/test_variant.h +++ b/tests/core/variant/test_variant.h @@ -1056,6 +1056,14 @@ TEST_CASE("[Variant] Identity comparison") { Variant obj_null_two_var = Variant((Object *)nullptr); CHECK(obj_null_one_var.identity_compare(obj_null_one_var)); CHECK(obj_null_one_var.identity_compare(obj_null_two_var)); + + Object *freed_one = new Object(); + Variant freed_one_var = freed_one; + delete freed_one; + Object *freed_two = new Object(); + Variant freed_two_var = freed_two; + delete freed_two; + CHECK_FALSE(freed_one_var.identity_compare(freed_two_var)); } TEST_CASE("[Variant] Nested array comparison") {