Skip to content

Commit 150a829

Browse files
committed
Added checks for observer_from_this after release/reset
1 parent df317e7 commit 150a829

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/runtime_tests.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,57 @@ TEST_CASE("observer from this after move assignment sealed", "[observer_from_thi
30553055
REQUIRE(mem_track.double_del() == 0u);
30563056
}
30573057

3058+
TEST_CASE("observer from this after release", "[observer_from_this]") {
3059+
memory_tracker mem_track;
3060+
3061+
{
3062+
test_ptr_from_this ptr1{new test_object_observer_from_this};
3063+
test_object_observer_from_this* ptr2 = ptr1.release();
3064+
const test_object_observer_from_this* cptr2 = ptr2;
3065+
3066+
test_optr_from_this optr_from_this = ptr2->observer_from_this();
3067+
test_optr_from_this_const optr_from_this_const = cptr2->observer_from_this();
3068+
3069+
REQUIRE(instances == 1);
3070+
REQUIRE(optr_from_this.expired() == true);
3071+
REQUIRE(optr_from_this_const.expired() == true);
3072+
REQUIRE(optr_from_this.get() == nullptr);
3073+
REQUIRE(optr_from_this_const.get() == nullptr);
3074+
3075+
delete ptr2;
3076+
}
3077+
3078+
REQUIRE(instances == 0);
3079+
REQUIRE(mem_track.leaks() == 0u);
3080+
REQUIRE(mem_track.double_del() == 0u);
3081+
}
3082+
3083+
TEST_CASE("observer from this after release and reset", "[observer_from_this]") {
3084+
memory_tracker mem_track;
3085+
3086+
{
3087+
test_ptr_from_this ptr1{new test_object_observer_from_this};
3088+
test_object_observer_from_this* ptr2 = ptr1.release();
3089+
const test_object_observer_from_this* cptr2 = ptr2;
3090+
3091+
test_ptr_from_this ptr3;
3092+
ptr3.reset(ptr2);
3093+
3094+
test_optr_from_this optr_from_this = ptr2->observer_from_this();
3095+
test_optr_from_this_const optr_from_this_const = cptr2->observer_from_this();
3096+
3097+
REQUIRE(instances == 1);
3098+
REQUIRE(optr_from_this.expired() == false);
3099+
REQUIRE(optr_from_this_const.expired() == false);
3100+
REQUIRE(optr_from_this.get() == ptr2);
3101+
REQUIRE(optr_from_this_const.get() == ptr2);
3102+
}
3103+
3104+
REQUIRE(instances == 0);
3105+
REQUIRE(mem_track.leaks() == 0u);
3106+
REQUIRE(mem_track.double_del() == 0u);
3107+
}
3108+
30583109
TEST_CASE("observer from this stack", "[observer_from_this]") {
30593110
memory_tracker mem_track;
30603111

0 commit comments

Comments
 (0)