diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-02 11:35:05 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-02 11:35:05 +0200 |
commit | 6db879ee663249041a6a731a8a6f31bd40ca8089 (patch) | |
tree | a388a35135da6eab58ccc949f93de411266701d7 /AK/Tests | |
parent | d9cc3e453cb6a7b5e7a3d7b477a497c8dee8f1f4 (diff) | |
download | serenity-6db879ee663249041a6a731a8a6f31bd40ca8089.zip |
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.
Diffstat (limited to 'AK/Tests')
-rw-r--r-- | AK/Tests/Makefile | 5 | ||||
-rw-r--r-- | AK/Tests/TestNonnullRefPtr.cpp | 36 |
2 files changed, 40 insertions, 1 deletions
diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile index 3847c4834e..f1a28322a9 100644 --- a/AK/Tests/Makefile +++ b/AK/Tests/Makefile @@ -1,4 +1,4 @@ -PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr +PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../ @@ -42,6 +42,9 @@ TestJSON: TestJSON.o $(SHARED_TEST_OBJS) TestWeakPtr: TestWeakPtr.o $(SHARED_TEST_OBJS) $(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestWeakPtr.o $(SHARED_TEST_OBJS) +TestNonnullRefPtr: TestNonnullRefPtr.o $(SHARED_TEST_OBJS) + $(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestNonnullRefPtr.o $(SHARED_TEST_OBJS) + clean: rm -f $(SHARED_TEST_OBJS) rm -f $(PROGRAMS) diff --git a/AK/Tests/TestNonnullRefPtr.cpp b/AK/Tests/TestNonnullRefPtr.cpp new file mode 100644 index 0000000000..9012206586 --- /dev/null +++ b/AK/Tests/TestNonnullRefPtr.cpp @@ -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) |