Brian
Brian

Reputation: 1723

C++ smart pointer for a non-object type?

I'm trying to use smart pointers such as 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 think that the storage variable is just a malloc'ed memory, not a C++ class object. Is there a way to use the smart pointers for the storage variable?

Thank you.

Upvotes: 2

Views: 530

Answers (2)

Georg Fritzsche
Georg Fritzsche

Reputation: 99122

shared_ptr allows you do specify a custom deallocator. However, looking at the documentation cvReleaseMemStorage() doesn't have the right form (void f(T*)) and you need a wrapper:

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

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

Upvotes: 9

Jacob
Jacob

Reputation: 3686

The shared_ptr class allows for you to provide a custom delete function/functor, you could simply wrap the cvReleaseMemStorage function in a function and provide that for shared_ptr along with the pointer you want it to manage for you?

Upvotes: 1

Related Questions