Ask Your Question
5

Can I perform nested operations with the Mat object?

asked 2012-09-12 17:26:16 -0500

ZachTM gravatar image

I am really new to OpenCV and I started out using the C structures. I had to release the images when I was done with them and I could not nest the operations I performed on the image. Now I am using Cv::Mat and I was wondering if I could do the following code with not memory problems:

Mat image;
transpose(imread( argv[1], 0 ),image);

Will this work without leaving any images stuck in the memory. I am also pretty new to C++ coming from a Java background so I appreciate any answers. Thank you!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-09-13 05:09:27 -0500

Michael Burdinov gravatar image

Well, yes you can do that and normaly it should not leak any memory, but it is not a very good practice. Mat is a smart pointer and as such should release its memory. But smart pointers may leak memory when initialized in function call. It is hard to create situation in which the memory is leaked, but it is possible. So it is better to spend extra line to read image and only after that to call transpose.

If you want to know more about how and why this may happen read 'Item 17' of 'Effective C++' 3rd edition: "Store newed objects in smart pointers in standalone statements".

edit flag offensive delete link more

Comments

Thank you for your answer!

ZachTM gravatar imageZachTM ( 2012-09-13 12:02:42 -0500 )edit
3

answered 2012-09-13 01:13:39 -0500

Vladislav Vinogradov gravatar image

Yes you can do this. Mat automatically manages memory via a reference-counting mechanism.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2012-09-12 17:26:16 -0500

Seen: 200 times

Last updated: Sep 13 '12