Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-01-04 13:38:35


Remigiusz Zukowski wrote:

[...]

> boost::function<void()> f2 = boost::bind( &class::method, wptr_from(
> some_weak_ptr ));
>
> This works, but is unsafe - after get_pointer succesfully returns
> pointer to the object, the object is unlocked and can be deleted during
> class::method execution. Can anyone help me solving this problem ?

You can use

template<class T> shared_ptr<T> sptr_from( weak_ptr<T> const & wpt )
{
    return shared_ptr<T>( wpt ); // throws on wpt.expired()
}

and then

boost::function<void()> f2 = boost::bind(
    &class::method, boost::bind( &sptr_from<class>, some_weak_ptr )
);

It's a bit more typing, though.


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