Boost logo

Boost Users :

From: edA-qa mort-ora-y (eda-qa_at_[hidden])
Date: 2005-03-24 15:48:41


I am in the process of converting a library of ours (which used a
Refcounting mechanism for shared objects) to be buildable on top of the
boost smart_ptr library*.

What feature I am having the most difficulty with is the ability for an
object to refer to itself and be properly shared. That is, by quick
example:

/////////////////////////////
class A {
     void Do( B* b )
         { b->Add( this ); } //trouble line
}

class B {
     vector::shared_ptr<A> items;
     void Add( A* a )
         { items.push_back( a ); }
}

...
shared_ptr<A> a( new A() );
shared_ptr<B> b( new B() );

a->Do( b ); //hidden problems
///////////////////////////////

In the trouble line I am having a problem since I need to refer to this,
but it will not be referring to the shared_ptr wrapper on the object.
Thus it will end up being delete'd twice.

Since I already need to derive from shared_ptr to implement additional
features I do have some possible options (such as each object containing
a weak reference to itself), but they all have particular drawbacks.

Is there a good method for resolving this problem?

*I'm doing this since I have no more interest in maintaining the
cross-platform aspects of our own library (though it works with PThreads
and Win32). And because I don't want to have to instrument my objects
with their own ref-counting (one of my solutions to the above however
requires this instrumenting nonetheless, but not on all objects).

-- 
edA-qa mort-ora-y
Idea Architect
http://disemia.com/

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net