A self-assignment leads to a call of release() with refcount being 2. In the release() method, refcount is decremented and then successfully checked for being 1. As a consequence, the underlying data is released. To prevent this, we test for a self-assignment
template<typename _Tp> inline Ptr<_Tp>& Ptr<_Tp>::operator = (const Ptr<_Tp>& _ptr)
{
- int* _refcount = _ptr.refcount;
- if( _refcount )
- CV_XADD(_refcount, 1);
- release();
- obj = _ptr.obj;
- refcount = _refcount;
+ if (this != &_ptr)
+ {
+ int* _refcount = _ptr.refcount;
+ if( _refcount )
+ CV_XADD(_refcount, 1);
+ release();
+ obj = _ptr.obj;
+ refcount = _refcount;
+ }
return *this;
}