Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV or Boost smart pointers

I have an expanding image processing project which relies heavily on the OpenCV library for much of its functionality, although I do also use a few boost functions as well.

I'd like to start using smart pointers to replace some raw pointers which are beginning to cause problems. My question is on which type of smart pointer to use, with my main choices (I think) being the OpenCV cv::Ptr or one of the boost variants.

I realise there are a number of questions explaining the different between each of the boost pointers, but I hoped somebody could offer an explanation of how cv::Ptr compares with them and make any recommendations of one or the other?

EDIT - I've noticed from the OpenCV docs that Ptr is similar to boost shared_ptr, is the essential difference just which library/include files are required?

like image 441
Chris Avatar asked Dec 19 '12 11:12

Chris


People also ask

Should I always use a smart pointer?

So, a smart pointer is only needed, when you use new or other means of dynamic memory allocation. In my opinion, you should prefer to allocate variables on the stack, so when refactoring code (to C++11), you should always ask yourself, if this new is needed, or could be replaced with an object on the stack.

Is CV mat a smart pointer?

The mostly used basic structure in OpenCV C++ is Mat . And the first thing to remember about Mat is to treat it like a smart pointer (like shared_ptr ) with the addition of some meta data (like cols rows etc), instead of an ordinary struct or container (like std::vector ).

What is the difference between the two smart pointers Shared_ptr and Unique_ptr?

Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.

What are smart pointers good for?

Smart pointers are used to make sure that an object is deleted if it is no longer used (referenced). The unique_ptr<> template holds a pointer to an object and deletes this object when the unique_ptr<> object is deleted.


1 Answers

For what I can see in OpenCV documentation, this is a reference-counted smart pointer, essentially, same as boost::shared_ptr. Even it uses atomic operations on the reference count.

I would make the choice based on portability and interoperability.

  1. Is your system going to be ported elsewhere and depends on OpenCV for sure but not on boost? Then, stick to OpenCV cv::Ptr if you can avoid boost and you get rid of the dependency.

  2. Does boost::shared_ptr plays nice with the rest of OpenCV? If you have something returning a cv::Ptr from OpenCV library, maybe it's better to stick to cv::Ptr in these cases, because the reference count will be handled incorrectly if you mix both kind of pointers and the resource could be destroyed prematurely.

  3. You're going to stick to boost wherever you port the project? Then, stick to boost::shared_ptr when you can do it, it's more standard, people know it and will immediately understand your code. UPDATE: In C++11 you have std::shared_ptr, which amounts to no dependency if you can afford it, so you can use std::shared_ptr in this case and get rid of boost also.

Just as a side note, there is a technique to mix boost and std shared pointers that can keep the reference correctly around and could be useful for someone. See this question, it could be relevant also for mixing other kind of reference-counted pointers: Conversion from boost::shared_ptr to std::shared_ptr?

In my experience, when you port something, the fewer dependencies, the better, or there are certain platforms for which compiling can be a hell. So make your choice based on portability if it's a concern and interoperability of pointers with libraries.

like image 58
Germán Diago Avatar answered Sep 22 '22 15:09

Germán Diago