From dc441f50cd546a79ecb5e4390411549325cac68e Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Mon, 31 Aug 2015 17:30:42 +0300 Subject: [PATCH] fixed memory leak in descriptor regression tests --- .../features2d/test/test_descriptors_regression.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/features2d/test/test_descriptors_regression.cpp b/modules/features2d/test/test_descriptors_regression.cpp index 2185625ae..a1c07f86b 100644 --- a/modules/features2d/test/test_descriptors_regression.cpp +++ b/modules/features2d/test/test_descriptors_regression.cpp @@ -61,7 +61,7 @@ static void writeMatInBin( const Mat& mat, const string& filename ) fwrite( (void*)&mat.rows, sizeof(int), 1, f ); fwrite( (void*)&mat.cols, sizeof(int), 1, f ); fwrite( (void*)&type, sizeof(int), 1, f ); - int dataSize = (int)(mat.step * mat.rows * mat.channels()); + int dataSize = (int)(mat.step * mat.rows); fwrite( (void*)&dataSize, sizeof(int), 1, f ); fwrite( (void*)mat.data, 1, dataSize, f ); fclose(f); @@ -80,12 +80,15 @@ static Mat readMatFromBin( const string& filename ) size_t elements_read4 = fread( (void*)&dataSize, sizeof(int), 1, f ); CV_Assert(elements_read1 == 1 && elements_read2 == 1 && elements_read3 == 1 && elements_read4 == 1); - uchar* data = (uchar*)cvAlloc(dataSize); - size_t elements_read = fread( (void*)data, 1, dataSize, f ); + Mat returnMat(rows, cols, type); + CV_Assert(returnMat.step * returnMat.rows == (size_t)(dataSize)); + + size_t elements_read = fread( (void*)returnMat.data, 1, dataSize, f ); CV_Assert(elements_read == (size_t)(dataSize)); + fclose(f); - return Mat( rows, cols, type, data ); + return returnMat; } return Mat(); }