summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-02 12:00:43 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-02 12:00:43 +0200
commit15866714da033763ccea4a866a2522d0ba88f1f9 (patch)
tree9345512a8584fc0264676da252e609a01db69aac /AK
parentcbc12728102e898da946edfd3a79fe476d02e95c (diff)
downloadserenity-15866714da033763ccea4a866a2522d0ba88f1f9.zip
AK: Add anti-null assertions in RefPtr.
This gives us better error messages when dereferencing null RefPtrs.
Diffstat (limited to 'AK')
-rw-r--r--AK/RefPtr.h32
1 files changed, 28 insertions, 4 deletions
diff --git a/AK/RefPtr.h b/AK/RefPtr.h
index 70dc84f97f..d698cfa261 100644
--- a/AK/RefPtr.h
+++ b/AK/RefPtr.h
@@ -49,18 +49,21 @@ public:
RefPtr(const NonnullRefPtr<T>& other)
: m_ptr(const_cast<T*>(other.ptr()))
{
+ ASSERT(m_ptr);
m_ptr->ref();
}
template<typename U>
RefPtr(const NonnullRefPtr<U>& other)
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
{
+ ASSERT(m_ptr);
m_ptr->ref();
}
template<typename U>
RefPtr(NonnullRefPtr<U>&& other)
: m_ptr(static_cast<T*>(&other.leak_ref()))
{
+ ASSERT(m_ptr);
}
template<typename U>
RefPtr(RefPtr<U>&& other)
@@ -121,6 +124,7 @@ public:
{
RefPtr tmp = move(other);
swap(tmp);
+ ASSERT(m_ptr);
return *this;
}
@@ -128,6 +132,7 @@ public:
{
RefPtr tmp = other;
swap(tmp);
+ ASSERT(m_ptr);
return *this;
}
@@ -136,6 +141,7 @@ public:
{
RefPtr tmp = other;
swap(tmp);
+ ASSERT(m_ptr);
return *this;
}
@@ -192,11 +198,29 @@ public:
T* ptr() { return m_ptr; }
const T* ptr() const { return m_ptr; }
- T* operator->() { return m_ptr; }
- const T* operator->() const { return m_ptr; }
+ T* operator->()
+ {
+ ASSERT(m_ptr);
+ return m_ptr;
+ }
+
+ const T* operator->() const
+ {
+ ASSERT(m_ptr);
+ return m_ptr;
+ }
- T& operator*() { return *m_ptr; }
- const T& operator*() const { return *m_ptr; }
+ T& operator*()
+ {
+ ASSERT(m_ptr);
+ return *m_ptr;
+ }
+
+ const T& operator*() const
+ {
+ ASSERT(m_ptr);
+ return *m_ptr;
+ }
operator const T*() const { return m_ptr; }
operator T*() { return m_ptr; }