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.
This commit is contained in:
Artur Wieczorek
2019-04-28 16:35:10 +02:00
parent 57aeaa3823
commit bd313b64ab
2 changed files with 36 additions and 4 deletions

View File

@@ -291,8 +291,11 @@ class WXDLLIMPEXP_PROPGRID wxPGAttributeStorage
{ {
public: public:
wxPGAttributeStorage(); wxPGAttributeStorage();
wxPGAttributeStorage(const wxPGAttributeStorage& other);
~wxPGAttributeStorage(); ~wxPGAttributeStorage();
wxPGAttributeStorage& operator=(const wxPGAttributeStorage& rhs);
void Set( const wxString& name, const wxVariant& value ); void Set( const wxString& name, const wxVariant& value );
unsigned int GetCount() const { return (unsigned int) m_map.size(); } unsigned int GetCount() const { return (unsigned int) m_map.size(); }
wxVariant FindValue( const wxString& name ) const wxVariant FindValue( const wxString& name ) const

View File

@@ -3208,19 +3208,48 @@ void wxPGChoices::Free()
// wxPGAttributeStorage // wxPGAttributeStorage
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
static inline void IncDataRef(wxPGHashMapS2P& map)
{
wxPGHashMapS2P::iterator it;
for ( it = map.begin(); it != map.end(); ++it )
{
static_cast<wxVariantData*>(it->second)->IncRef();
}
}
static inline void DecDataRef(wxPGHashMapS2P& map)
{
wxPGHashMapS2P::iterator it;
for ( it = map.begin(); it != map.end(); ++it )
{
static_cast<wxVariantData*>(it->second)->DecRef();
}
}
wxPGAttributeStorage::wxPGAttributeStorage() wxPGAttributeStorage::wxPGAttributeStorage()
{ {
} }
wxPGAttributeStorage::wxPGAttributeStorage(const wxPGAttributeStorage& other)
{
m_map = other.m_map;
IncDataRef(m_map);
}
wxPGAttributeStorage::~wxPGAttributeStorage() 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; DecDataRef(m_map);
data->DecRef(); m_map = rhs.m_map;
IncDataRef(m_map);
} }
return *this;
} }
void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value ) void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )