From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Fri, 12 Jun 2009 18:24:37 +0000 (+0000)
Subject: fix memory leak in SetColFormat() if the column already had an attribute (closes... 
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4a53f7010a8b65790c2e053ec8374244be2d85ad

fix memory leak in SetColFormat() if the column already had an attribute (closes #1807)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61029 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---

diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp
index cc03db9a3f..dccf054460 100644
--- a/src/generic/grid.cpp
+++ b/src/generic/grid.cpp
@@ -643,30 +643,30 @@ void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
     {
         if ( attr )
         {
-            // add the attribute - no need to do anything to reference count
-            //                     since we take ownership of the attribute.
+            // store the new attribute, taking its ownership
             m_rowsOrCols.Add(rowOrCol);
             m_attrs.Add(attr);
         }
         // nothing to remove
     }
-    else
+    else // we have an attribute for this row or column
     {
         size_t n = (size_t)i;
-        if ( m_attrs[n] == attr )
-            // nothing to do
-            return;
+
+        // notice that this code works correctly even when the old attribute is
+        // the same as the new one: as we own of it, we must call DecRef() on
+        // it in any case and this won't result in destruction of the new
+        // attribute if it's the same as old one because it must have ref count
+        // of at least 2 to be passed to us while we keep a reference to it too
+        m_attrs[n]->DecRef();
+
         if ( attr )
         {
-            // change the attribute, handling reference count manually,
-            //                       taking ownership of the new attribute.
-            m_attrs[n]->DecRef();
+            // replace the attribute with the new one
             m_attrs[n] = attr;
         }
-        else
+        else // remove the attribute
         {
-            // remove this attribute, handling reference count manually
-            m_attrs[n]->DecRef();
             m_rowsOrCols.RemoveAt(n);
             m_attrs.RemoveAt(n);
         }