[Xorp-hackers] On replacing ref_ptr with shared_ptr

Bruce Simpson bms at incunabulum.net
Sun Nov 15 01:30:54 PST 2009


What I'd really like to do is to be able to replace ref_ptr<T> with 
shared_ptr<T>/weak_ptr<T>, although this is a non-trivial refactoring.
The code typedefs ref_ptr<T> away in a number of places, so it's not 
100% obvious where it is being used.

The callback code also uses ref_ptr<T> extensively -- that's probably a 
candidate for the Boost mixin enable_shared_from_this<T>, which is now 
part of C++0x TR1.

It is used to embed a weak_ptr<T> to 'this' in every class which is 
potentially shared as a refcounted object. The object can then use the 
shared_from_this<T>() template method member to obtain a shared_ptr<T> 
to itself without bumping the refcount itself.

The reason Boost users usually do this is to return the shared_ptr<T> to 
the object in situations where it would otherwise return or pass its 
'this' pointer.
We don't want the object to embed a shared_ptr<T> to itself, because 
this means there is a dangling refcount; this is why the embedded 
weak_ptr<T> is declared 'mutable'.

There are a number of places in the code where we use refcounting, or we 
need it and it isn't there; the ref_ptr<T>& semantics are potentially 
harmful, as the intent isn't clear, especially for folk who are either 
new to the code, or are getting back into it after being away for months.



More information about the Xorp-hackers mailing list