Ask Your Question

Revision history [back]

Release external data pointer in Cv::Mat constructor

I have these following lines of code:

cv::Mat obj(size.cy, size.cx, CV_8UC3, dst_ptr/*, (size.cx * channels + 3) & -4*/);
source.copyTo(obj);
StretchDIBits( hdc, 0, 0, rect.right, rect.bottom, 0, 0, arr.cols, arr.rows, dst_ptr, binfo, DIB_RGB_COLORS, SRCCOPY );

These lines cause memory leak in my program. As document:

Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it

So how can I release the external data? Indeed it's the void *dst_ptr var