From: Tom Titchener (Tom.Titchener_at_[hidden])
Date: 2006-09-22 13:28:17
(apologies for starting a new response thread cloned from this, which I confess I lost in my inbox)
> I'd like to build a chain of objects with an upper layer holding a
> shared_ptr to a lower layer and the lower layer holding a weak_ptr back
> to the upper layer. The upper layer users the shared_ptr for control. The
> lower layer uses the weak_ptr for callbacks (first calling lock to
> convert it to a shared_ptr). And ... I'd like to do it in my class ctors.
> That's the kicker. It *seems* like the shared_from_this framework should
> give me what I want...
>> I can't solve your particular problem, but it seems to be that using a
>> weak pointer is the wrong solution in your example code. The callback is a
>> member of the control, and thus cannot outlive it. In other words, the
>> weak pointer ought to be superfluous - use an ordinary pointer.
>> If the callback can actually outlive the control, then it shouldn't be the
>> control that creates it. External code should create the callback and pass
>> it the shared_ptr to an existing control.
>> Sebastian Redl
I don't think I specified my problem adequately. The layers are part of a communications framework that terminates at an API that manages a thread pool from which notifications are delivered. My goal is to use the weak_ptr -> shared_ptr conversion to independently manage the lifetime of the upper layers. If the conversion fails, the lower layer knows to cleanup context and continue.
I'd like to leverage the thread safety of the conversion for a simple design that doesn't require a monitor to coordinate object lifetimes. I can achieve this by adding a binding step to the layer instantiation. But I'd rather do it through the ctors, if I could just figure out how to jigger the ref count framework...
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk