When the cv::Mat object goes out of scope, the memory allocated is automatically released. This is very convenient because you avoid having problems with memory leaks. Moreover, the cv::Mat class implements reference counting and shallow copy such that when an image is assigned to another one, the image data (that is the pixels) is not copied, and both images will point to the same memory block. This also applies to images passed by value or returned by value. A reference count is kept such that the memory will be released only when all of the references to the image will be destructed. If you wish to create an image that will contain a new copy of the original image, you will use the method copyTo() . (查看原文)
When the ima local variable goes out of scope, this variable is de-allocated, but since the associated reference counter indicates that its internal image data is being referred by another instance (that is the gray variable) its memory block is not released. (查看原文)
还没人写过短评呢