From bd313b64ab1e25cb6397120e2fb3150fdec35dc3 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 28 Apr 2019 16:35:10 +0200 Subject: [PATCH] Fix copying wxPGAttributeStorage We need implement copy ctor and assignment operator because we are going to do a shallow copy of wxPGHashMapS2P data member and therefore we have to manually update reference counters of the objects being referenced in this map. --- include/wx/propgrid/property.h | 3 +++ src/propgrid/property.cpp | 37 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index ad3be58c15..0822136402 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -291,8 +291,11 @@ class WXDLLIMPEXP_PROPGRID wxPGAttributeStorage { public: wxPGAttributeStorage(); + wxPGAttributeStorage(const wxPGAttributeStorage& other); ~wxPGAttributeStorage(); + wxPGAttributeStorage& operator=(const wxPGAttributeStorage& rhs); + void Set( const wxString& name, const wxVariant& value ); unsigned int GetCount() const { return (unsigned int) m_map.size(); } wxVariant FindValue( const wxString& name ) const diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index 9cc04632b3..8d8f0f525b 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -3208,19 +3208,48 @@ void wxPGChoices::Free() // wxPGAttributeStorage // ----------------------------------------------------------------------- +static inline void IncDataRef(wxPGHashMapS2P& map) +{ + wxPGHashMapS2P::iterator it; + for ( it = map.begin(); it != map.end(); ++it ) + { + static_cast(it->second)->IncRef(); + } +} + +static inline void DecDataRef(wxPGHashMapS2P& map) +{ + wxPGHashMapS2P::iterator it; + for ( it = map.begin(); it != map.end(); ++it ) + { + static_cast(it->second)->DecRef(); + } +} + wxPGAttributeStorage::wxPGAttributeStorage() { } +wxPGAttributeStorage::wxPGAttributeStorage(const wxPGAttributeStorage& other) +{ + m_map = other.m_map; + IncDataRef(m_map); +} + wxPGAttributeStorage::~wxPGAttributeStorage() { - wxPGHashMapS2P::iterator it; + DecDataRef(m_map); +} - for ( it = m_map.begin(); it != m_map.end(); ++it ) +wxPGAttributeStorage& wxPGAttributeStorage::operator=(const wxPGAttributeStorage& rhs) +{ + if ( this != &rhs ) { - wxVariantData* data = (wxVariantData*) it->second; - data->DecRef(); + DecDataRef(m_map); + m_map = rhs.m_map; + IncDataRef(m_map); } + return *this; } void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )