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.
@@ -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);
@@ -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()
+ m_being_destroyed = true;
if (m_link)
m_link->m_ptr = nullptr;
}
private:
RefPtr<WeakLink<T>> m_link;
+ bool m_being_destroyed { false };
};