Boost logo

Boost Users :

From: Phil Endecott (spam_from_boost_users_at_[hidden])
Date: 2006-12-10 18:47:40


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.


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