Fix reference count bug in SharedPtr container

This commit is contained in:
Vinnie Falco
2013-09-06 20:06:17 -07:00
parent b921791b34
commit 4b7b5a41a2

View File

@@ -189,9 +189,7 @@ public:
*/
~SharedPtr ()
{
if (m_p != nullptr)
if (m_p->decReferenceCount (false))
ContainerDeletePolicy <T>::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 <class U>
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 <T> old (
this->swap (acquire (u)));
release (this->swap (acquire (u)));
return *this;
}