I can\'t seem to figure out how to do this, I have searched on google and found two code examples, one from a github screen capture library and another on a posting group and ne
From the OpenCV documentation
data – 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, i.e. 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, user should take care of it.
I think this is the point, using this constructor does not copy the buffer you are passing to the constructor, so you should not free this data until you no longer need the cv::Mat. Also I know next to nothing about this stuff, but why are you passing the BITMAPINFOHEADER and BITMAPFILEHEADER to your cv::Mat object, that doesn't seem right at all.