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.
This commit is contained in:
Andreas Kling 2020-01-25 10:14:40 +01:00
parent b0192cfb38
commit 3f52cee595
Notes: sideshowbarker 2024-07-19 09:50:42 +09:00
2 changed files with 10 additions and 1 deletions

View File

@ -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);

View File

@ -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
};
}