mirror of
https://github.com/opencv/opencv.git
synced 2025-06-12 20:42:53 +08:00
Fix a memory leak in the Mat copying constructor
Exception may be rasied inside the body of a copying constructor after refcount has been increased, and beacause in the case of the exception destrcutor is never called what causes memory leak. This commit adds a workaround that calls the release() function before the exception is thrown outside the contructor.
This commit is contained in:
parent
d25ee8a273
commit
b638aa74d7
@ -484,21 +484,31 @@ Mat::Mat(const Mat& m, const Range& _rowRange, const Range& _colRange)
|
|||||||
}
|
}
|
||||||
|
|
||||||
*this = m;
|
*this = m;
|
||||||
if( _rowRange != Range::all() && _rowRange != Range(0,rows) )
|
try
|
||||||
{
|
{
|
||||||
CV_Assert( 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows );
|
if( _rowRange != Range::all() && _rowRange != Range(0,rows) )
|
||||||
rows = _rowRange.size();
|
{
|
||||||
data += step*_rowRange.start;
|
CV_Assert( 0 <= _rowRange.start && _rowRange.start <= _rowRange.end
|
||||||
flags |= SUBMATRIX_FLAG;
|
&& _rowRange.end <= m.rows );
|
||||||
}
|
rows = _rowRange.size();
|
||||||
|
data += step*_rowRange.start;
|
||||||
|
flags |= SUBMATRIX_FLAG;
|
||||||
|
}
|
||||||
|
|
||||||
if( _colRange != Range::all() && _colRange != Range(0,cols) )
|
if( _colRange != Range::all() && _colRange != Range(0,cols) )
|
||||||
|
{
|
||||||
|
CV_Assert( 0 <= _colRange.start && _colRange.start <= _colRange.end
|
||||||
|
&& _colRange.end <= m.cols );
|
||||||
|
cols = _colRange.size();
|
||||||
|
data += _colRange.start*elemSize();
|
||||||
|
flags &= cols < m.cols ? ~CONTINUOUS_FLAG : -1;
|
||||||
|
flags |= SUBMATRIX_FLAG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(...)
|
||||||
{
|
{
|
||||||
CV_Assert( 0 <= _colRange.start && _colRange.start <= _colRange.end && _colRange.end <= m.cols );
|
release();
|
||||||
cols = _colRange.size();
|
throw;
|
||||||
data += _colRange.start*elemSize();
|
|
||||||
flags &= cols < m.cols ? ~CONTINUOUS_FLAG : -1;
|
|
||||||
flags |= SUBMATRIX_FLAG;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( rows == 1 )
|
if( rows == 1 )
|
||||||
|
Loading…
Reference in New Issue
Block a user