From a5d4a39e6d7eaa20171cd9fc6da8b2cf7828a1bd Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 13 Apr 2007 15:52:27 +0100 Subject: [PATCH] Fix Ptr::m_count memory leak in some places. --- src/core/ptr.h | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/core/ptr.h b/src/core/ptr.h index 8fe1db397..b44071c3b 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -130,39 +130,38 @@ Ptr::DeallocCount (uint32_t *count) template Ptr::Ptr () - : m_ptr (0), - m_count (Ptr::AllocCount ()) + : m_ptr (0) {} template Ptr::Ptr (T *ptr) - : m_ptr (ptr), - m_count (Ptr::AllocCount ()) + : m_ptr (ptr) { if (m_ptr != 0) { + m_count = Ptr::AllocCount (); *m_count = 1; } } template Ptr::Ptr (Ptr const&o) - : m_ptr (o.m_ptr), - m_count (o.m_count) + : m_ptr (o.m_ptr) { if (m_ptr != 0) { + m_count = o.m_count; (*m_count)++; } } template template Ptr::Ptr (Ptr const &o) - : m_ptr (o.m_ptr), - m_count (o.m_count) + : m_ptr (o.m_ptr) { if (m_ptr != 0) { + m_count = o.m_count; (*m_count)++; } } @@ -185,10 +184,8 @@ template Ptr & Ptr::operator = (Ptr const& o) { - if (o.m_ptr != 0) - { - (*(o.m_count))++; - } + if (&o == this) + return *this; if (m_ptr != 0) { (*m_count)--; @@ -199,7 +196,11 @@ Ptr::operator = (Ptr const& o) } } m_ptr = o.m_ptr; - m_count = o.m_count; + if (m_ptr != 0) + { + m_count = o.m_count; + (*m_count)++; + } return *this; } @@ -246,10 +247,18 @@ template T * Ptr::Remove (void) { - NS_ASSERT ((*m_count) == 1); - T *retval = m_ptr; - m_ptr = 0; - return retval; + if (m_ptr == 0) + { + return (T *) 0; + } + else + { + NS_ASSERT ((*m_count) == 1); + Ptr::DeallocCount (m_count); + T *retval = m_ptr; + m_ptr = 0; + return retval; + } } // non-member friend functions.