]> git.saurik.com Git - wxWidgets.git/commitdiff
fix reference counting problems with wxGridCellWithAttr (re-#9567)
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 13 Jun 2008 21:37:58 +0000 (21:37 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 13 Jun 2008 21:37:58 +0000 (21:37 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54199 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/grid/griddemo.cpp
src/generic/grid.cpp

index 363fa804726154a6e31e0d0cb2669864096f9bb1..bd254bfdd4b4aea360027c24730e4a9b2db9ea54 100644 (file)
@@ -360,6 +360,12 @@ GridFrame::GridFrame()
     grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
     grid->SetCellValue(7, 1, _T("Big box!"));
 
+    // this does exactly nothing except testing that SetAttr() handles NULL
+    // attributes and does reference counting correctly
+    grid->SetAttr(11, 11, NULL);
+    grid->SetAttr(11, 11, new wxGridCellAttr);
+    grid->SetAttr(11, 11, NULL);
+
     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
     topSizer->Add( grid,
                    1,
index 69debf772705481ca6244420c1e3dd2da89ffafd..fc5c4e9a87c4b14e738ad4997d2fd8d7201c9b39 100644 (file)
@@ -70,6 +70,24 @@ struct wxGridCellWithAttr
     wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
         : coords(row, col), attr(attr_)
     {
+        wxASSERT( attr );
+    }
+
+    wxGridCellWithAttr(const wxGridCellWithAttr& other)
+        : coords(other.coords),
+          attr(other.attr)
+    {
+        attr->IncRef();
+    }
+
+    wxGridCellWithAttr& operator=(const wxGridCellWithAttr& other)
+    {
+        coords = other.coords;
+        attr->DecRef();
+        attr = other.attr;
+        attr->IncRef();
+
+        return *this;
     }
 
     ~wxGridCellWithAttr()
@@ -79,10 +97,6 @@ struct wxGridCellWithAttr
 
     wxGridCellCoords coords;
     wxGridCellAttr  *attr;
-
-// Cannot do this:
-//  DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
-// without rewriting the macros, which require a public copy constructor.
 };
 
 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray,
@@ -2597,11 +2611,8 @@ void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
         }
         //else: nothing to do
     }
-    else
+    else // we already have an attribute for this cell
     {
-        // free the old attribute
-        m_attrs[(size_t)n].attr->DecRef();
-
         if ( attr )
         {
             // change the attribute
@@ -2654,8 +2665,6 @@ void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
                 else
                 {
                     // ...or remove the attribute
-                    // No need to DecRef the attribute itself since this is
-                    // done be wxGridCellWithAttr's destructor!
                     m_attrs.RemoveAt(n);
                     n--;
                     count--;
@@ -2690,8 +2699,6 @@ void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
                 else
                 {
                     // ...or remove the attribute
-                    // No need to DecRef the attribute itself since this is
-                    // done be wxGridCellWithAttr's destructor!
                     m_attrs.RemoveAt(n);
                     n--;
                     count--;
@@ -3168,7 +3175,8 @@ void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
 {
     if ( m_attrProvider )
     {
-        attr->SetKind(wxGridCellAttr::Cell);
+        if ( attr )
+            attr->SetKind(wxGridCellAttr::Cell);
         m_attrProvider->SetAttr(attr, row, col);
     }
     else