|
Boost Users : |
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-12-12 11:54:23
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_at_[hidden] [mailto:james.jones_at_[hidden]]
Sent: Tuesday, December 12, 2006 4:58 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] shared_ptr with custom free function
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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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