1
Fork 0
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:
Andreas Kling 2019-08-02 11:35:05 +02:00
parent d9cc3e453c
commit 6db879ee66
3 changed files with 44 additions and 4 deletions

View 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)