From 4b7b5a41a2133ac680f6c01515f8e2c46866a018 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 6 Sep 2013 20:06:17 -0700 Subject: [PATCH] Fix reference count bug in SharedPtr container --- .../beast/modules/beast_core/memory/SharedPtr.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Subtrees/beast/modules/beast_core/memory/SharedPtr.h b/Subtrees/beast/modules/beast_core/memory/SharedPtr.h index ac12f89d6..fc7613138 100644 --- a/Subtrees/beast/modules/beast_core/memory/SharedPtr.h +++ b/Subtrees/beast/modules/beast_core/memory/SharedPtr.h @@ -189,9 +189,7 @@ public: */ ~SharedPtr () { - if (m_p != nullptr) - if (m_p->decReferenceCount (false)) - ContainerDeletePolicy ::destroy (m_p); + release (m_p); } /** Returns `true` if the container is not pointing to an object. */ @@ -227,7 +225,7 @@ public: } private: - // Acquire a reference to u for the caller. + // Acquire a reference to u for the caller if not null. // template static T* acquire (U* u) noexcept @@ -237,6 +235,14 @@ private: return u; } + // Release a reference to t if not null. + // + static void release (T* t) noexcept + { + if (t != nullptr) + t->decReferenceCount (); + } + // Swap ownership of the currently referenced object. // The caller receives a pointer to the original object, // and this container is left with the passed object. No @@ -257,8 +263,7 @@ private: SharedPtr& assign (U* u) { if (m_p != u) - SharedPtr old ( - this->swap (acquire (u))); + release (this->swap (acquire (u))); return *this; }