summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-09-21 18:07:46 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-09-21 18:07:46 +0200
commit9e00651e1440263ff78aa29474229e5c18f06053 (patch)
tree63185b005bad81208421a1e1b72454926b778cf7
parent4d8455156efd6514be5ef55b49f487d8ebc15fda (diff)
downloadserenity-9e00651e1440263ff78aa29474229e5c18f06053.zip
LibCore: ObjectPtr should delete the pointee when cleared
We were only deleting the pointee when the ObjectPtr was destroyed. If the ObjectPtr is cleared before that, we should also delete the pointee. This is not the most important class to get right, since it will go away as soon as we're able to switch to RefPtr.
-rw-r--r--Libraries/LibCore/ObjectPtr.h18
1 files changed, 17 insertions, 1 deletions
diff --git a/Libraries/LibCore/ObjectPtr.h b/Libraries/LibCore/ObjectPtr.h
index 98a4cd2bf0..ef41e509be 100644
--- a/Libraries/LibCore/ObjectPtr.h
+++ b/Libraries/LibCore/ObjectPtr.h
@@ -18,8 +18,20 @@ public:
}
~ObjectPtr()
{
+ clear();
+ }
+
+ void clear()
+ {
if (m_ptr && !m_ptr->parent())
delete m_ptr;
+ m_ptr = nullptr;
+ }
+
+ ObjectPtr& operator=(std::nullptr_t)
+ {
+ clear();
+ return *this;
}
template<typename U>
@@ -52,13 +64,17 @@ public:
ObjectPtr& operator=(const ObjectPtr& other)
{
- m_ptr = other.m_ptr;
+ if (this != &other) {
+ clear();
+ m_ptr = other.m_ptr;
+ }
return *this;
}
ObjectPtr& operator=(ObjectPtr&& other)
{
if (this != &other) {
+ clear();
m_ptr = exchange(other.m_ptr, nullptr);
}
return *this;