Smart Pointers OpenCV or Boost

I have an advanced image processing project that relies heavily on the OpenCV library for most of its functionality, although I also use several boost functions.

I would like to start using smart pointers to replace some of the original pointers that are starting to cause problems. My question is what type of smart pointer to use, with my main choice (I think) is OpenCV cv::Ptr or one of the boost options.

I understand that there is a number of questions explaining the differences between each of the forcing pointers, but I was hoping someone could offer an explanation of how cv::Ptr compares with them and makes any recommendations for one or the other?

EDIT - I noticed from OpenCV docs that Ptr is similar to boost shared_ptr , is it a significant difference just which library files include files?

+7
source share
1 answer

For what I see in the OpenCV documentation, this is a smart pointer with reference counting, essentially the same as boost::shared_ptr . Even it uses atomic operations in the reference counter.

I would make a choice based on portability and interoperability.

  • Will your system be moved to another place and, of course, depends on OpenCV, but not on boost ? Then stick with OpenCV cv::Ptr if you can avoid the boost and get rid of the dependency.

  • Is boost::shared_ptr with the rest of OpenCV? If you have something returning cv :: Ptr from the OpenCV library, it might be better to stick to cv :: Ptr in these cases, because the reference counter will not work correctly if you mix both types of pointers and the resource may be prematurely destroyed .

  • Are you going to stick with boost wherever you port your project? Then, stick with boost::shared_ptr , when you can do this, this is more standard, people know this and immediately understand your code. UPDATE: In C ++ 11, you have std :: shared_ptr, which has no dependency if you can afford it, so you can use std :: shared_ptr in this case and get rid of boost.

As a note, there is a method for mixing boost and std with common pointers that can properly support links and can be useful to someone. See this question, it may be relevant for mixing other pointers with reference counting: Conversion from boost :: shared_ptr to std :: shared_ptr?

In my experience, when you mess up something, the fewer dependencies, the better, or there are certain platforms for which compilation can be hell. Therefore, make your choice based on portability if this is a problem and pointer compatibility with libraries.

+7
source

All Articles