
In addition, You could specify a custom deleter, which is supported by the shared_ptr. In your case it can be something like: struct Deleter { void operator ()(blob* pblob) { free_blob(pblob); } }; shared_ptr<blob> blob_ptr(create_blob(), Deleter()); Good Luck, Ovanes -----Original Message----- From: james.jones@firstinvestors.com [mailto:james.jones@firstinvestors.com] Sent: Tuesday, December 12, 2006 4:58 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] shared_ptr with custom free function From: "Phil Endecott" <spam_from_boost_users@chezphil.org>
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?
I would think one solution would be simply to wrap your C struct in a C++ class with a constructor and a destructor, then use shared_ptr on the C++ struct. Example: C: struct blob { ... }; blob * create_blob (); void free_blob(blob *); C++: class foo { public: foo() : data(create_blob()) {} ~foo() { free_blob(data); } private: blob * data; }; Now use shared_ptr< foo > as you usually would. This also gives you the flexibility NOT to use shared_ptr if you choose. - James Jones Administrative Data Mgmt. Webmaster 375 Raritan Center Pkwy, Suite A Data Architect Edison, NJ 08837 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users