Ask Your Question

Should I release cv::Ptr before reassigning?

asked 2018-06-14 14:29:36 -0500

mhaghighat gravatar image

updated 2018-06-14 14:36:37 -0500

Say, I have a

cv::Ptr<cv::backgroundsubtractormog2> mogPtr = cv::createBackgroundSubtractorMOG2(300, 16, true);

Later, I'd like to change the configuration of the background subtractor, but I want to reassign the new object pointer to the same variable (mogPtr):

mogPtr = cv::createBackgroundSubtractorMOG2(300, 64, false);

I wonder if I should release the mogPtr before I assign a new pointer to it. Is it a bad idea if I do? Will there be a memory leak if I don't?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-14 14:46:36 -0500

LBerger gravatar image

updated 2018-06-14 14:52:26 -0500

An doc says :

Template class for smart pointers with shared ownership.

A Ptr<t> pretends to be a pointer to an object of type T. Unlike an ordinary pointer, however, the object will be automatically cleaned up once all Ptr instances pointing to it are destroyed.

Ptr is similar to boost::shared_ptr that is part of the Boost library ( and std::shared_ptr from the C++11 standard.


and to modify parameters you can use method setVarThreshold

You don't need to release object because opencv will do it

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-06-14 14:29:36 -0500

Seen: 473 times

Last updated: Jun 14 '18