mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
AK: Fix ref leak in NonnullRefPtr::operator=(T&).
We would leak a ref when assigning a T& to a NonnullRefPtr that already contains that same T.
This commit is contained in:
parent
d9cc3e453c
commit
6db879ee66
3 changed files with 44 additions and 4 deletions
36
AK/Tests/TestNonnullRefPtr.cpp
Normal file
36
AK/Tests/TestNonnullRefPtr.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <AK/TestSuite.h>
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
struct Object : public RefCounted<Object> {
|
||||
int x;
|
||||
};
|
||||
|
||||
TEST_CASE(basics)
|
||||
{
|
||||
auto 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)
|
||||
{
|
||||
auto 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