From efc1c39315cfacff250a0d936c2110b9968efb9e Mon Sep 17 00:00:00 2001 From: PhilLab Date: Fri, 8 Aug 2014 17:39:12 +0200 Subject: [PATCH] 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 --- modules/core/include/opencv2/core/operations.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp index 4ab7e35f9..ed32a07a2 100644 --- a/modules/core/include/opencv2/core/operations.hpp +++ b/modules/core/include/opencv2/core/operations.hpp @@ -2625,12 +2625,15 @@ template inline Ptr<_Tp>::Ptr(const Ptr<_Tp>& _ptr) template 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; }