Fix self-assignment bug in wxObjectDataPtr
wxObjectDataPtr::operator=() didn't handle self-assignment correctly, i.e. could delete the object when doing "x = x". Fix this by incrementing the reference count before decrementing it on possibly the same object to ensure that it never reaches 0.
This commit is contained in:
@@ -331,22 +331,26 @@ public:
|
|||||||
|
|
||||||
wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy)
|
wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy)
|
||||||
{
|
{
|
||||||
|
// Take care to increment the reference first to ensure correct
|
||||||
|
// behaviour in case of self-assignment.
|
||||||
|
T* const ptr = tocopy.m_ptr;
|
||||||
|
if (ptr)
|
||||||
|
ptr->IncRef();
|
||||||
if (m_ptr)
|
if (m_ptr)
|
||||||
m_ptr->DecRef();
|
m_ptr->DecRef();
|
||||||
m_ptr = tocopy.m_ptr;
|
m_ptr = ptr;
|
||||||
if (m_ptr)
|
|
||||||
m_ptr->IncRef();
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
wxObjectDataPtr& operator=(const wxObjectDataPtr<U> &tocopy)
|
wxObjectDataPtr& operator=(const wxObjectDataPtr<U> &tocopy)
|
||||||
{
|
{
|
||||||
|
T* const ptr = tocopy.get();
|
||||||
|
if (ptr)
|
||||||
|
ptr->IncRef();
|
||||||
if (m_ptr)
|
if (m_ptr)
|
||||||
m_ptr->DecRef();
|
m_ptr->DecRef();
|
||||||
m_ptr = tocopy.get();
|
m_ptr = ptr;
|
||||||
if (m_ptr)
|
|
||||||
m_ptr->IncRef();
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user