memory management of Mat

asked 2015-09-10 09:27:06 -0500

sarjoondeen gravatar image

I read in the below link about the Mat variables about how it allocates and deallocates memory

In the link, its mentioned that Mat is that you no longer need to manually allocate its memory and release it as soon as you do not need it

What I don't understand it how and exactly when it will de-allocate the memory of a Mat.

Suppose I am running my Opencv Application.

Whether it will de-allocate its memory when I exit the running Application(when I press Ctrl+C)

Please clarify me on this.

Thanks in advance Sarjoondeen.

edit retag flag offensive close merge delete



basically when the Mat is not needed anymore (runs out of scope for example). Ctrl+C'ing is a special case, and not so much related to this because in that case the OS is releasing all memory in use by the killed process.

boaz001 gravatar imageboaz001 ( 2015-09-10 10:35:49 -0500 )edit

cv::Mat holds an internal refcount, which gets increased, once you assign/copy it, like Mat a=b; and gets decreased, when an instance goes out of scope(like: leaving a pair of {} braces)

if the refcount goes down to zero, it will destroy itself, very similar to a std::shared_ptr

so, to use it properly, avoid any C constructs, like raw pointers to that (you'll inevitably thrash the refcount)

again this is c++, not C.

berak gravatar imageberak ( 2015-09-10 12:38:40 -0500 )edit

Thanks For you reply.. I get it now..

sarjoondeen gravatar imagesarjoondeen ( 2015-09-18 02:55:10 -0500 )edit