Search code examples
c++boostsmart-pointerscircular-dependencyweak

boost::shared_ptr cycle break with weak_ptr


I am currently in a situation like:

struct A {  
  shared_ptr<B> b;  
};  
struct B {  
  shared_ptr<A> a;  
};

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

I know this won't work, because the references would continue to point to each other. I've also been told that weak_ptr solves this issue.

However, weak_ptr has no get or -> overload. I've heard mentions of 'use lock()', but can anyone give code examples of how to do this correctly?


Solution

  • Come on now.

    http://boost.org/doc/libs/1_42_0/libs/smart_ptr/weak_ptr.htm

    ^^^^^ EXAMPLE IS RIGHT THERE ^^^^^^

    DAMN!