Browse Source

AK: Assert if trying to create a WeakPtr to an object being destroyed

Trying to make_weak_ptr() on something that has begun destruction is
very unlikely to be what you want. Let's assert if that scenario comes
up so we can catch it immediately.
Andreas Kling 5 years ago
parent
commit
3f52cee595
2 changed files with 10 additions and 1 deletions
  1. 3 0
      AK/WeakPtr.h
  2. 7 1
      AK/Weakable.h

+ 3 - 0
AK/WeakPtr.h

@@ -88,6 +88,9 @@ private:
 template<typename T>
 inline WeakPtr<T> Weakable<T>::make_weak_ptr()
 {
+#ifdef DEBUG
+    ASSERT(!m_being_destroyed);
+#endif
     if (!m_link)
         m_link = adopt(*new WeakLink<T>(static_cast<T&>(*this)));
     return WeakPtr<T>(m_link);

+ 7 - 1
AK/Weakable.h

@@ -27,8 +27,8 @@
 #pragma once
 
 #include "Assertions.h"
-#include "RefPtr.h"
 #include "RefCounted.h"
+#include "RefPtr.h"
 
 namespace AK {
 
@@ -66,12 +66,18 @@ protected:
 
     ~Weakable()
     {
+#ifdef DEBUG
+        m_being_destroyed = true;
+#endif
         if (m_link)
             m_link->m_ptr = nullptr;
     }
 
 private:
     RefPtr<WeakLink<T>> m_link;
+#ifdef DEBUG
+    bool m_being_destroyed { false };
+#endif
 };
 
 }