Fix Ptr<T>::m_count memory leak in some places.
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Fri, 13 Apr 2007 15:52:27 +0100
changeset 426 7d0bde915fd6
parent 425 851ab3d13928
child 431 2a342cacddf4
Fix Ptr<T>::m_count memory leak in some places.
src/core/ptr.h
--- a/src/core/ptr.h	Wed Apr 18 07:44:43 2007 +0200
+++ b/src/core/ptr.h	Fri Apr 13 15:52:27 2007 +0100
@@ -130,39 +130,38 @@
 
 template <typename T>
 Ptr<T>::Ptr ()
-  : m_ptr (0),
-    m_count (Ptr::AllocCount ())
+  : m_ptr (0)
 {}
 
 template <typename T>
 Ptr<T>::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 <typename T>
 Ptr<T>::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 <typename T>
 template <typename U>
 Ptr<T>::Ptr (Ptr<U> 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 @@
 Ptr<T> &
 Ptr<T>::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 @@
         }
     }
   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 @@
 T *
 Ptr<T>::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.