C ++ smart pointer for type without object?

I am trying to use smart pointers like auto_ptr, shared_ptr. However, I don't know how to use it in this situation.

CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);

      

I'm not sure, but I believe the storage variable is just malloc memory, not a C ++ class object. Is there a way to use smart pointers for a storage variable?

Thanks.

+2


a source to share


2 answers


shared_ptr

allows you to specify a custom deactivator. However, looking at the documentation is cvReleaseMemStorage()

not in the correct form ( void f(T*)

) and you need a wrapper:



void myCvReleaseMemStorage(CvMemStorage* p) {
   cvReleaseMemStorage(&p);
}

shared_ptr<CvMemStorage> sp(cvCreateMemStorage(), &myCvReleaseMemStorage);

      

+9


a source


The class shared_ptr

allows you to provide a custom function / remove functor, you can just wrap the function cvReleaseMemStorage

in a function and provide that for shared_ptr

along with the pointer you want it to work for you



+1


a source







All Articles