Fixed self-assignment in cv::Ptr::operator =

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
pull/3058/head
PhilLab 11 years ago
parent 3334b1437b
commit efc1c39315
  1. 15
      modules/core/include/opencv2/core/operations.hpp

@ -2625,12 +2625,15 @@ template<typename _Tp> inline Ptr<_Tp>::Ptr(const Ptr<_Tp>& _ptr)
template<typename _Tp> inline Ptr<_Tp>& Ptr<_Tp>::operator = (const Ptr<_Tp>& _ptr) template<typename _Tp> inline Ptr<_Tp>& Ptr<_Tp>::operator = (const Ptr<_Tp>& _ptr)
{ {
int* _refcount = _ptr.refcount; if (this != &_ptr)
if( _refcount ) {
CV_XADD(_refcount, 1); int* _refcount = _ptr.refcount;
release(); if( _refcount )
obj = _ptr.obj; CV_XADD(_refcount, 1);
refcount = _refcount; release();
obj = _ptr.obj;
refcount = _refcount;
}
return *this; return *this;
} }

Loading…
Cancel
Save