cv::Mat's release method

Bryanyan picture Bryanyan · Apr 8, 2013 · Viewed 21k times · Source

I would like to confirm whether cv::Mat::release() method is similar to free() in C programming, i.e., its deallocates the Matrix data from the memory.

In particular, I would like to understand the behaviour of this method with respect to memory leaks and make sure there is no leak in may programs.

Answer

Daniel Martín picture Daniel Martín · Apr 8, 2013

If the reference counter is equal to one, then yes, cv::Mat::release() will decrement it to zero and deallocate the structure (like free in C).

If the reference counter is greater than one (ie. there's some other object interested in the structure), then cv::Mat::release() will only decrement the reference counter.

You can increment the reference counter of a cv::Mat structure (that is, to flag that you are interested in it and you don't want it to be deallocated) by invoking the cv::Mat::addref() method.