Ask Your Question
1

Is it safe to use cv::Ptr<> within STL containers?

asked 2013-02-05 08:23:15 -0600

SR gravatar image

AFAIK std::auto_ptr<> is not suitable for STL containers.

Is it safe to use cv::Ptr<> within STL containers e.g. as std::vector< cv::Ptr<MyObject> >?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-02-05 10:20:25 -0600

sammy gravatar image

updated 2013-02-06 04:55:01 -0600

The auto_ptr<> template has a number of issues, that make it unsuitable for a range of applications.

However, the cv::Ptr<> class is a full-fledged smart-pointer template, similar to boost::shared_ptr<>. You can use it safely in containers, and with some care and limitations, across threads.

Here is a more detailed explanation on smart pointers

http://answers.opencv.org/question/535/is-there-penalty-for-reference-counting-in-mat/#538

Update on thread safety

(Copied from boost library docs - cv::Ptr<> and boost::shared_ptr<> are very similar in construction and usage)

shared_ptr objects offer the same level of thread safety as built-in types. A shared_ptr instance can be "read" (accessed using only const operations) simultaneously by multiple threads. Different shared_ptr instances can be "written to" (accessed using mutable operations such as operator= or reset) simultaneosly by multiple threads (even when these instances are copies, and share the same reference count underneath.)

Any other simultaneous accesses result in undefined behavior.

edit flag offensive delete link more

Comments

Thank you, could you please elaborate on "with some care and limitations". What do you mean exactly?

SR gravatar imageSR ( 2013-02-06 01:57:51 -0600 )edit

Updated answer

sammy gravatar imagesammy ( 2013-02-06 04:55:52 -0600 )edit

Thank you!

SR gravatar imageSR ( 2013-02-06 06:41:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-02-05 08:23:15 -0600

Seen: 1,540 times

Last updated: Feb 06 '13