class WXDLLEXPORT wxTextCtrl;
 class WXDLLEXPORT wxSpinCtrl;
 
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
+#define wxSafeIncRef(p) if ( p ) (p)->IncRef()
+#define wxSafeDecRef(p) if ( p ) (p)->DecRef()
+
 // ----------------------------------------------------------------------------
 // wxGridCellRenderer: this class is responsible for actually drawing the cell
 // in the grid. You may pass it to the wxGridCellAttr (below) to change the
 class WXDLLEXPORT wxGridCellRenderer
 {
 public:
+    wxGridCellRenderer() { m_nRef = 1; }
+
+    // this class is ref counted: it is created with ref count of 1, so
+    // calling DecRef() once will delete it. Calling IncRef() allows to lock
+    // it until the matching DecRef() is called
+    void IncRef() { m_nRef++; }
+    void DecRef() { if ( !--m_nRef ) delete this; }
+
     // draw the given cell on the provided DC inside the given rectangle
     // using the style specified by the attribute and the default or selected
     // state corresponding to the isSelected value.
                                wxDC& dc,
                                int row, int col) = 0;
 
-    // virtual dtor for any base class
+protected:
+    // virtual dtor for any base class - private because only DecRef() can
+    // delete us
     virtual ~wxGridCellRenderer();
+
+private:
+    size_t m_nRef;
+
+    // suppress the stupid gcc warning about the class having private dtor and
+    // no friends
+    friend class wxGridCellRendererDummyFriend;
 };
 
 // the default renderer for the cells containing string data
 {
 public:
     wxGridCellEditor();
-    virtual ~wxGridCellEditor();
+
+    // this class is ref counted: it is created with ref count of 1, so
+    // calling DecRef() once will delete it. Calling IncRef() allows to lock
+    // it until the matching DecRef() is called
+    void IncRef() { m_nRef++; }
+    void DecRef() { if ( !--m_nRef ) delete this; }
 
     bool IsCreated() { return m_control != NULL; }
 
     virtual void Destroy();
 
 protected:
+    // the dtor is private because only DecRef() can delete us
+    virtual ~wxGridCellEditor();
+
+    // the ref count - when it goes to 0, we die
+    size_t m_nRef;
+
     // the control we show on screen
     wxControl*  m_control;
 
     wxColour m_colFgOld,
              m_colBgOld;
     wxFont m_fontOld;
+
+    // suppress the stupid gcc warning about the class having private dtor and
+    // no friends
+    friend class wxGridCellEditorDummyFriend;
 };
 
 // the editor for string/text data
         SetAlignment(hAlign, vAlign);
     }
 
-    // creates a new copy of this object: warning, this is destructive copy
-    // (this is why it's non const), the renderer and editor are "given to"
-    // the new object
-    wxGridCellAttr *Clone();
+    // creates a new copy of this object
+    wxGridCellAttr *Clone() const;
 
     // this class is ref counted: it is created with ref count of 1, so
     // calling DecRef() once will delete it. Calling IncRef() allows to lock
     // it until the matching DecRef() is called
     void IncRef() { m_nRef++; }
     void DecRef() { if ( !--m_nRef ) delete this; }
-    void SafeIncRef() { if ( this ) IncRef(); }
-    void SafeDecRef() { if ( this ) DecRef(); }
 
     // setters
     void SetTextColour(const wxColour& colText) { m_colText = colText; }
 
     // takes ownership of the pointer
     void SetRenderer(wxGridCellRenderer *renderer)
-        { delete m_renderer; m_renderer = renderer; }
+        { wxSafeDecRef(m_renderer); m_renderer = renderer; }
     void SetEditor(wxGridCellEditor* editor)
-        { delete m_editor; m_editor = editor; }
+        { wxSafeDecRef(m_editor); m_editor = editor; }
 
     // accessors
     bool HasTextColour() const { return m_colText.Ok(); }
     }
 
     // the dtor is private because only DecRef() can delete us
-    ~wxGridCellAttr() { delete m_renderer; delete m_editor; }
+    ~wxGridCellAttr()
+    {
+        wxSafeDecRef(m_renderer);
+        wxSafeDecRef(m_editor);
+    }
 
     // the ref count - when it goes to 0, we die
     size_t   m_nRef;
 
         : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
         { }
 
-    ~wxGridDataTypeInfo() { delete m_renderer; delete m_editor; }
+    ~wxGridDataTypeInfo()
+    {
+        wxSafeDecRef(m_renderer);
+        wxSafeDecRef(m_editor);
+    }
 
     wxString            m_typeName;
     wxGridCellRenderer* m_renderer;
 wxGridCellEditor::wxGridCellEditor()
 {
     m_control = NULL;
+
+    m_nRef = 1;
 }
 
 
 // wxGridCellAttr
 // ----------------------------------------------------------------------------
 
-wxGridCellAttr *wxGridCellAttr::Clone()
+wxGridCellAttr *wxGridCellAttr::Clone() const
 {
     wxGridCellAttr *attr = new wxGridCellAttr;
     if ( HasTextColour() )
     if ( m_renderer )
     {
         attr->SetRenderer(m_renderer);
-        m_renderer = NULL;
+        m_renderer->IncRef();
     }
     if ( m_editor )
     {
         attr->SetEditor(m_editor);
-        m_editor = NULL;
+        m_editor->IncRef();
     }
 
     if ( IsReadOnly() )
                                           wxGridCellRenderer* renderer,
                                           wxGridCellEditor* editor)
 {
-    int loc;
     wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
 
     // is it already registered?
-    if ((loc = FindDataType(typeName)) != -1) {
+    int loc = FindDataType(typeName);
+    if ( loc != wxNOT_FOUND )
+    {
         delete m_typeinfo[loc];
         m_typeinfo[loc] = info;
     }
-    else {
+    else
+    {
         m_typeinfo.Add(info);
     }
 }
     {
         // as we take ownership of the pointer and don't store it, we must
         // free it now
-        attr->SafeDecRef();
+        wxSafeDecRef(attr);
     }
 }
 
     {
         // as we take ownership of the pointer and don't store it, we must
         // free it now
-        attr->SafeDecRef();
+        wxSafeDecRef(attr);
     }
 }
 
     {
         // as we take ownership of the pointer and don't store it, we must
         // free it now
-        attr->SafeDecRef();
+        wxSafeDecRef(attr);
     }
 }
 
 wxGrid::~wxGrid()
 {
     ClearAttrCache();
-    m_defaultCellAttr->SafeDecRef();
+    wxSafeDecRef(m_defaultCellAttr);
 
 #ifdef DEBUG_ATTR_CACHE
     size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
 {
     wxGridCellAttr *attr = GetCellAttr(row, col);
     wxColour colour = attr->GetBackgroundColour();
-    attr->SafeDecRef();
+    attr->DecRef();
     return colour;
 }
 
 {
     wxGridCellAttr *attr = GetCellAttr(row, col);
     wxColour colour = attr->GetTextColour();
-    attr->SafeDecRef();
+    attr->DecRef();
     return colour;
 }
 
 {
     wxGridCellAttr *attr = GetCellAttr(row, col);
     wxFont font = attr->GetFont();
-    attr->SafeDecRef();
+    attr->DecRef();
     return font;
 }
 
 {
     wxGridCellAttr *attr = GetCellAttr(row, col);
     attr->GetAlignment(horiz, vert);
-    attr->SafeDecRef();
+    attr->DecRef();
 }
 
 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
 {
     if ( m_attrCache.row != -1 )
     {
-        m_attrCache.attr->SafeDecRef();
+        wxSafeDecRef(m_attrCache.attr);
         m_attrCache.row = -1;
     }
 }
     self->m_attrCache.row = row;
     self->m_attrCache.col = col;
     self->m_attrCache.attr = attr;
-    attr->SafeIncRef();
+    wxSafeIncRef(attr);
 }
 
 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
     if ( row == m_attrCache.row && col == m_attrCache.col )
     {
         *attr = m_attrCache.attr;
-        (*attr)->SafeIncRef();
+        wxSafeIncRef(m_attrCache.attr);
 
 #ifdef DEBUG_ATTR_CACHE
         gs_nAttrCacheHits++;
     }
     else
     {
-        attr->SafeDecRef();
+        wxSafeDecRef(attr);
     }
 }
 
     }
     else
     {
-        attr->SafeDecRef();
+        wxSafeDecRef(attr);
     }
 }
 
 
     wxCoord extent, extentMax = 0;
     int max = column ? m_numRows : m_numCols;
-    for ( int rowOrCol = 0; rowOrCol < m_numRows; rowOrCol++ )
+    for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
     {
         if ( column )
             row = rowOrCol;
     if ( column )
         SetColSize(col, extentMax);
     else
-        SetRowSize(col, extentMax);
+        SetRowSize(row, extentMax);
 
     if ( setAsMin )
     {
         if ( column )
             SetColMinimalWidth(col, extentMax);
         else
-            SetRowMinimalHeight(col, extentMax);
+            SetRowMinimalHeight(row, extentMax);
     }
 }