Boost logo

Boost :

From: Frank Mori Hess (fmhess_at_[hidden])
Date: 2008-04-26 12:23:14


On Saturday 26 April 2008 09:56, Daniel Frey wrote:
> On Sat, 2008-04-26 at 15:45 +0300, Peter Dimov wrote:
> > Are you sure that you need to test for emptiness instead of
> > expiration?
>
> I'm not sure, because I don't know what is expected. I initially
> used .use_count() != 0, but it triggered a problem in
> esft_constructor_test.cpp, line 131. I already asked Frank for more
> information/documentation about what (the new) esft should support, but
> he said that there is no documentation, instead he pointed me to the
> regression tests, so I figured I need to make them pass. With .empty(),
> they do.
>
> When looking at the test case that fails with .use_count() != 0, I don't
> really see why the call to shared_from_this() in line 130 should throw.
> Shouldn't the call alone be legal? The only illegal thing I could spot
> in this test case is, that after calling x.shared_from_this() no other
> shared_ptr takes ownership of x.

Yes, it seemed safer to make the shared_ptr from shared_from_this() calls
share ownership with a well-defined external shared_ptr (that is, the
first shared_ptr to take ownership). That seemed to minimize the
ambiguity and possibility of dangling shared_ptr. The behavior of the
classic esft is different, shared_from_this() returns shared_ptr that
share ownership with the last shared_ptr to take ownership. Another
possibility, which you seem to be suggesting, would be to always handle
the "no owner exists" case by having shared_from_this() return shared_ptr
that will share ownership with the next external shared_ptr to take
ownership. In any case, the exact behavior of shared_from_this wrt
multiple owners has never been specified as far as I know.

-- 
Frank



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk