|
Boost Users : |
From: james.jones_at_[hidden]
Date: 2006-12-12 10:58:19
From: "Phil Endecott" <spam_from_boost_users_at_[hidden]>
> 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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net