The following program snippet tests the behavior of Mat::release()
(adapted from opencv-org-answer)
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
cout << "test_mat_release.cpp" << endl;
{
//memory is released
const char *pzInputImg = "data/em_sample.png";
Mat img;
img = imread(pzInputImg);
assert(img.data);
img.release();
assert(!img.data);
}
{
//memory is released
Mat img(100, 100, CV_32FC1);
assert(img.data);
img.release();
assert(!img.data);
}
{
//Since Mat does not owns data , data is not released
//you have to explicitly release data
int cols=17;
int rows=15;
unsigned char * data = new unsigned char[rows*cols*3];
Mat img(rows, cols, CV_8UC3, data);
assert(img.data);
img.release();
assert(!img.data);
delete [] data;
}
Mat imgRef;
{
//memory is not released
//since there img.data is now shared with imgRef
Mat img(100, 100, CV_32FC1);
imgRef=img;
assert(img.data);
assert(img.data == imgRef.data);
img.release();
assert(img.empty());
assert(!img.data);
//img.data is NULL, but imgRef.data is not NULL, so data is not de-allocated
assert(!imgRef.empty());
assert(imgRef.data);
}
return 0;
}