Weak_ptr and parent-child cyclic dependencies?

Since by the time the destructor for the the child gets called, the parent's destructor has already run (dtors for member objects get run after the dtor for the containing object), even if the child was holding a plain pointer to the parent, calling the parent's member function would be invalid by the time ~Child() was called You might be able to work around this by having Child call getSystem() at some earlier point and cache the result. Maybe in the constructor of Child (if it has a reference to the parent at the time) or maybe there can be an interface added so that Parent can let the child know it needs to collect whatever it might need during destruction from the parent at that time I understand that neither of these is a great solution (it increases coupling of the objects) -hopefully someone will post a better option.

Since by the time the destructor for the the child gets called, the parent's destructor has already run (dtors for member objects get run after the dtor for the containing object), even if the child was holding a plain pointer to the parent, calling the parent's member function would be invalid by the time ~Child() was called. You might be able to work around this by having Child call getSystem() at some earlier point and cache the result. Maybe in the constructor of Child (if it has a reference to the parent at the time) or maybe there can be an interface added so that Parent can let the child know it needs to collect whatever it might need during destruction from the parent at that time.

I understand that neither of these is a great solution (it increases coupling of the objects) -hopefully someone will post a better option.

Removing the circular reference is preferable, but if you cannot you can force Child to be destroyed before Parent is completely gone. In the destructor, explicitly call reset() on Child. This will force it to be destroyed immediately, assuming there are no other shared_ptrs to it.

Warning, if Parent is actually a base class all of it's subclasses will alreayd have been destroyed. Virtual function calls will probably not behave as expected.

I believe the member you're looking for is clear, not reset :) – Billy ONeal Dec 18 '10 at 1:39 This sounds really similar to standard memory management :) arr smart pointers... – jameszhao00 Dec 18 '10 at 1:47 @Bill ONeal: Nope, pretty sure it is reset in Boost (which I believe is the most common implementation of shared_ptr). Does the C++0x implemention have a different interface? – Chris Dec 18 '10 at 2:32 Sorry --- thought that was a vector of children.

:P – Billy ONeal Dec 18 '10 at 5:32.

First rule of weak_ptr: always check the locking (returned pointer or exception): after all the real reason to use weak_ptr is that it doesn't control the life-cycle of the pointed object.

Just from the code you posted this should work. The only thing deleting _child is the parent class. So there are two possibilites: First, something else also has a reference to the _child pointer, and keeps it ref count alive, and then parent is destroyed.

Then eventually whatever else is holding onto child is also destroyed, killing off the child then. Scenario 2 is that the call to getSystem depends on some other members youre not showing us, and those are getting deleted before the _child shared_ptr is.

It doesn't work because the child's destruction is instigated by the parent's destruction (the child is owned by the parent). So by the time the child's destructor is called, the parent's destructor has already been called (and the parent is still in the process of destructing). – nobar Feb 25 at 5:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions