diff --git a/include/wx/object.h b/include/wx/object.h index f210ec0faf..dd1e2fb927 100644 --- a/include/wx/object.h +++ b/include/wx/object.h @@ -313,6 +313,13 @@ public: m_ptr = ptr; } + T* release() + { + T* const ptr = m_ptr; + m_ptr = NULL; + return ptr; + } + wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy) { if (m_ptr) diff --git a/interface/wx/object.h b/interface/wx/object.h index 3497e94eb7..653fda5942 100644 --- a/interface/wx/object.h +++ b/interface/wx/object.h @@ -608,6 +608,21 @@ public: */ void reset(T *ptr); + /** + Release the owned pointer, making caller responsible for decrementing + its reference count. + + This method should be used only for interoperating with the existing + code working with raw pointers, typically when returning a raw pointer + from a function. + + After calling this function, this object becomes invalid, i.e. it + doesn't hold any valid pointer value any more. + + @since 3.1.4 + */ + T* release(); + /** Conversion to a boolean expression (in a variant which is not convertable to anything but a boolean expression). diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 52400f3eaf..b02ef8c0f9 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -1596,41 +1596,34 @@ void GridFrame::OnBugsTable(wxCommandEvent& ) // ---------------------------------------------------------------------------- MyGridCellAttrProvider::MyGridCellAttrProvider() + : m_attrForOddRows(new wxGridCellAttr) { - m_attrForOddRows = new wxGridCellAttr; m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY); } -MyGridCellAttrProvider::~MyGridCellAttrProvider() -{ - m_attrForOddRows->DecRef(); -} - wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const { - wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind); + wxObjectDataPtr + attr(wxGridCellAttrProvider::GetAttr(row, col, kind)); if ( row % 2 ) { if ( !attr ) { attr = m_attrForOddRows; - attr->IncRef(); } else { if ( !attr->HasBackgroundColour() ) { - wxGridCellAttr *attrNew = attr->Clone(); - attr->DecRef(); - attr = attrNew; + attr = attr->Clone(); attr->SetBackgroundColour(*wxLIGHT_GREY); } } } - return attr; + return attr.release(); } void GridFrame::OnVTable(wxCommandEvent& ) diff --git a/samples/grid/griddemo.h b/samples/grid/griddemo.h index a80c366b7b..68319b204a 100644 --- a/samples/grid/griddemo.h +++ b/samples/grid/griddemo.h @@ -296,13 +296,12 @@ class MyGridCellAttrProvider : public wxGridCellAttrProvider { public: MyGridCellAttrProvider(); - virtual ~MyGridCellAttrProvider(); virtual wxGridCellAttr *GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) const wxOVERRIDE; private: - wxGridCellAttr *m_attrForOddRows; + wxObjectDataPtr m_attrForOddRows; }; // ----------------------------------------------------------------------------