From 3f52cee59559ac1b32e14c513b10dc8ba20b6801 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 Jan 2020 10:14:40 +0100 Subject: [PATCH] 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. --- AK/WeakPtr.h | 3 +++ AK/Weakable.h | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index da4b8f9642..bec76e7d96 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -88,6 +88,9 @@ private: template inline WeakPtr Weakable::make_weak_ptr() { +#ifdef DEBUG + ASSERT(!m_being_destroyed); +#endif if (!m_link) m_link = adopt(*new WeakLink(static_cast(*this))); return WeakPtr(m_link); diff --git a/AK/Weakable.h b/AK/Weakable.h index f29ce9f835..85762db437 100644 --- a/AK/Weakable.h +++ b/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> m_link; +#ifdef DEBUG + bool m_being_destroyed { false }; +#endif }; }