From: Eric Niebler (eric_at_[hidden])
Date: 2005-06-06 11:45:28
Lorenzo Bettini wrote:
> Jonathan Wakely wrote:
>> On Mon, Jun 06, 2005 at 03:58:41PM +0200, Lorenzo Bettini wrote:
>>> However, it is not clear to me how to use weak_ptr in this context...
>>> can some one provide some clue, please?
> the main problem I had is
> just "ensure the shared_ptrs that own the objects are still
> around when you use the weak_ptrs.".
> Moreover, I had to deal with mutual dependences, and this makes the
> whole thing worse...
A couple of options, none of them very pretty.
1) Ask Peter Dimov to give you his experimental cycle collection code
for shared_ptr. Last I saw, this had exception safety issues because it
didn't provide a no-throw guarantee, which you would need if you were to
use it from a destructor (which is likely).
2) Grab tracking_ptr.hpp from
boost-sandbox/boost/xpressive/detail/utility (can also be found in
xpressive.zip from the Boost Sandbox File Vault).
I had to solve the same problem for xpressive, where regex objects can
embed other regex objects, either "by value" or "by reference". This
leads to all sorts of interesting cyclic ref-count scenarios. I use
tracking_ptr to handle this.
Unfortunately, tracking_ptr is not documented. I forces you to separate
your object into a handle-body, where the body inherits from
enable_reference_tracking<Body>, and the handle holds a
tracking_ptr<Body>. There's more, but maybe you can figure out the rest
by seeing how xpressive uses tracking_ptr.
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk