smart_ptr and self-referencing objects?
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/
edA-qa mort-ora-y wrote:
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 ///////////////////////////////
You want to use enable_shared_from_this:
/////////////////////////////
class A : enable_shared_from_this<A> {
void Do( shared_ptr<B> b )
{ b->Add( shared_from_this() ); }
}
class B {
vector
Eric Niebler wrote:
void Do( shared_ptr<B> b ) { b->Add( shared_from_this() ); } You can read about enable_shared_from_this in the shared_ptr docs.
Thank you, that's basically a more elegant form of the solution I was going to do. I'll use that. :) -- edA-qa mort-ora-y Idea Architect http://disemia.com/
edA-qa mort-ora-y
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.
Have a look at enable_shared_from_this: http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
edA-qa
-
edA-qa mort-ora-y
-
Eric Niebler