Fixed self-assignment in cv::Ptr::operator =
authorPhilLab <PhilLab@users.noreply.github.com>
Fri, 8 Aug 2014 15:39:12 +0000 (17:39 +0200)
committerPhilLab <PhilLab@users.noreply.github.com>
Fri, 8 Aug 2014 15:39:12 +0000 (17:39 +0200)
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

modules/core/include/opencv2/core/operations.hpp

index 4ab7e35..ed32a07 100644 (file)
@@ -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)
 {
-    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;
 }