mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:27:42 +00:00
AK: Fix ref leaks in RefPtr assignment operators.
Many of the RefPtr assignment operators would cause ref leaks when we call them to assign a pointer that's already the one kept.
This commit is contained in:
parent
06f82901b7
commit
cbc1272810
3 changed files with 89 additions and 37 deletions
60
AK/Tests/TestRefPtr.cpp
Normal file
60
AK/Tests/TestRefPtr.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <AK/TestSuite.h>
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
struct Object : public RefCounted<Object> {
|
||||
int x;
|
||||
};
|
||||
|
||||
TEST_CASE(basics)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
EXPECT(object.ptr() != nullptr);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
object->ref();
|
||||
EXPECT_EQ(object->ref_count(), 2);
|
||||
object->deref();
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
|
||||
{
|
||||
NonnullRefPtr another = *object;
|
||||
EXPECT_EQ(object->ref_count(), 2);
|
||||
}
|
||||
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_reference)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
object = *object;
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_ptr)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
object = object.ptr();
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_moved_self)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
object = move(object);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_copy_self)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
object = object;
|
||||
EXPECT_EQ(object->ref_count(), 1);
|
||||
}
|
||||
|
||||
TEST_MAIN(String)
|
Loading…
Add table
Add a link
Reference in a new issue