Boost logo

Boost :

From: Carl Daniel (cpdaniel_at_[hidden])
Date: 2002-04-18 08:16:52


"David B. Held" <dheld_at_[hidden]> wrote in message
news:a9limo$9jc$1_at_main.gmane.org...
> I have uploaded
http://groups.yahoo.com/group/boost/files/intrusive_ptr.hpp,
> which is an implementation of an intrusive reference-counted
> smart pointer that was shamelessly copied from boost::shared_ptr.

A couple of comments: I, like you, need to create smart pointers from this
frequently, and I also dislike the separate allocation of the reference
count. In fact, I've stayed away from boost::shared_ptr entirely for those
reasons & instead rolled my own intrusive ref-counted pointer several times.

One thing I'd change, although this does come with a cost, is to make the
objects self-deleting. This requires making the destructor of ref_counted
virtual, and putting a "delete this;" in ref_counted::release(). I know
some (many?) people dislike this idiom because 'delete this' feels so wrong,
but it's actually a very good idiom, especially in environments where it's
possible to have multiple heaps in a single program. In such situations,
it's essential that the delete expression use the heap from which the object
was created. To complete the pattern, I always use a factory (function,
class, whatever) with such classes, and make both the constructor(s) and
destructor private.

Maybe it's just my COM background influencing me... but this strategy has
worked very well for me in many large projects.

-cd


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