mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:47:34 +00:00
AK: NonnullRefPtr should allow assigning owner to ownee
Given the following situation: struct Object : public RefCounted<Object> { RefPtr<Object> parent; } NonnullRefPtr<Object> object = get_some_object(); object = *object->parent; We would previously crash if 'object' was the only strongly referencing pointer to 'parent'. This happened because NonnullRefPtr would unref the outgoing pointee before reffing the incoming pointee. This patch fixes that by implementing NonnullRefPtr assignments using pointer swaps, just like RefPtr already did.
This commit is contained in:
parent
4e6fe3c14b
commit
7ea264a660
2 changed files with 40 additions and 27 deletions
|
@ -59,4 +59,18 @@ TEST_CASE(assign_reference)
|
|||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_owner_of_self)
|
||||
{
|
||||
struct Object : public RefCounted<Object> {
|
||||
RefPtr<Object> parent;
|
||||
};
|
||||
|
||||
auto parent = adopt(*new Object);
|
||||
auto child = adopt(*new Object);
|
||||
child->parent = move(parent);
|
||||
|
||||
child = *child->parent;
|
||||
EXPECT_EQ(child->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_MAIN(String)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue