Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] deleter that destroys shared_ptr - is itsafe?
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-09-08 18:21:55


Igor R wrote:
> Hello,
>
> Is the following code legal and safe -
> a) according to the shared_ptr specs

I think so. There is nothing in the specs that prohibits it.

> b) with the current boost::shared_ptr implementation

It should be fine. shared_ptr_test contains a similar test case:

namespace n_report_1
{

class foo
{
public:

    foo(): m_self(this)
    {
    }

    void suicide()
    {
        m_self.reset();
    }

private:

    boost::shared_ptr<foo> m_self;
};

void test()
{
    foo * foo_ptr = new foo;
    foo_ptr->suicide();
}

} // namespace n_report_1

> struct a
> {
> a() : p_(this, mem_fn(&a::delete_me))
> {}
> void reset()
> {
> p_.reset();
> }
> void delete_me()
> {
> delete this;
> }
>
> shared_ptr<a> p_;
> };
>
>
> int main()
> {
> a* p = new a;
> p->reset();
> }


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