[prev in list] [next in list] [prev in thread] [next in thread] 

List:       boost-users
Subject:    [Boost-users] boost::weak_ptr and boost::intrusive_ptr
From:       "Steven T. Hatton" <hattons () globalsymmetry ! com>
Date:       2006-12-01 12:03:13
Message-ID: 200612010703.13886.hattons () globalsymmetry ! com
[Download RAW message or body]

Why is there no counterpart for boost::weak_ptr that works with 
boost::intrusive_ptr?  It's certainly possible to set up cycles with 
boost::intrusive_ptr.  A simple example where intrusive_ptr is the right 
thing to use, and where the problem arrises is a tree structure.  It is 
sometimes useful to maintain a bidirectional pointer relationship between 
children and parent nodes in a tree.  Ideally, deleting the root of a tree 
will cascade down to delete all intermediate and leaf nodes which are not 
referenced by some other object.  If both the downward and upward pointers 
are intrusive, removing the pointer to the root will result in orphaned data 
because the upward pointers prevent the root node from being deleted, while 
the downward pointers prevent the child nodes (and their upward pointers) 
from being removed.  

Intrusive pointers are, IMO, the correct mechanism to hold a tree together.  
They are efficient, effective, and easy to understand.  They work better than 
shared pointers for recursive data structures.  Unfortunately the lack of a 
corresponding weak_ptr to go with  boost::intrusive_ptr means I either use a 
raw pointer to the parent, devise a strategy for decrementing the pointers 
myself, or create my own weak pointer.  All of these options detract 
significantly from the value of boost::intrusive_ptr.

I have the impression that in the past people have used intrusive pointers for 
the "wrong things", and this has caused them to get an overall bad 
reputation.  IMO, they do have a place in good software design.

Any thoughts on this topic?

Steven

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic