shared_ptr with custom free function

Dear Boost experts, I have a C library that returns pointers to structs, and has special free functions that I must call to finalise and free these structs. Is there any way that I can create a shared_ptr that will call this special library free function when the last copy is destroyed? I was hoping that there would be some way of achieving this by overloading operator delete, but it seems not. Any ideas? Here's a more concrete example, in case it's not obvious: class Foo { public: Foo(struct blob* b): my_blob(b) {} ~Foo() {free_blob(my_blob);} private: struct blob* my_blob; }; Foo is not copyable, because the both copies will free the blob when they are destroyed. So: class Foo2 { public: Foo(struct blob* b): my_blob(b) {} private: boost::shared_ptr<struct blob> my_blob; }; Foo2 can be copied and the blob will be deleted exactly once when the last copy is destroyed, but the library's free_blob() function has not been used. inline void operator delete(struct blob* b) { free_blob(b); } Doesn't work; that's not how overloading of operator delete works. Thanks for any suggestions. Phil.

Phil Endecott wrote:
Dear Boost experts,
I have a C library that returns pointers to structs, and has special free functions that I must call to finalise and free these structs. Is there any way that I can create a shared_ptr that will call this special library free function when the last copy is destroyed?
Yes, there is: shared_ptr< blob > pb( create_blob(), free_blob );
participants (2)
-
Peter Dimov
-
Phil Endecott