Smart pointers - passing 'this' around

B

Bonzo

Another smart pointer problem. I have a tree structure. The idea is for
parents to have SmartPtr's to the children, with children holding a weak
pointer (WeakPtr) back to the parent. That's great, but how do I
implement the link from child to parent?

Here's where the problem occurs:

SmartPtr<ContainerNode>
ContainerNode :: CreateChildContainer()
{
SmartPtr<ContainerNode> child( new ContainerNode );

// a) This would cause 'this' to be deallocated at end of scope!
// SmartPtr<ContainerNode> smart( this );
// child->SetParent( smart );

// b) Can't assign raw ptr to WeakPtr in SetParent.
// child->SetParent( this );

return child;
}

So how do I pass 'this' to the child? A hack to bump up the strong ref
count in case a) so SmartPtr won't deallocate 'this'? Ugh.


Instead, the calling code could of course say:

SmartPtr<ContainerNode> parent;
SmartPtr<ContainerCode> child;
....
child = parent->CreateChildContainer();
child->SetParent( parent );

and it would work fine. The WeakPtr can be assigned to from the
SmartPtr. But the caller shouldn't have to do that.

Or am I wrong in assuming I shouldn't be putting raw pointers into weak
pointers? WeakPtr::getPtr() would return NULL since there'd never be a
strong ref to the ptr.

This seems like a ridiculous thing to be stumbling on, but I'm not sure
what to do. Thanks for any help.

-jim
 
C

Cy Edmunds

Bonzo said:
Another smart pointer problem. I have a tree structure. The idea is for
parents to have SmartPtr's to the children, with children holding a weak
pointer (WeakPtr) back to the parent. That's great, but how do I
implement the link from child to parent?

Why not just use a raw pointer? Smart pointers are generally used for memory
management, but I don't suppose you want the children managing memory for
the parents. (Although sometimes it works that way in real life. heh.)
Here's where the problem occurs:

SmartPtr<ContainerNode>
ContainerNode :: CreateChildContainer()
{
SmartPtr<ContainerNode> child( new ContainerNode );

// a) This would cause 'this' to be deallocated at end of scope!
// SmartPtr<ContainerNode> smart( this );
// child->SetParent( smart );

// b) Can't assign raw ptr to WeakPtr in SetParent.
// child->SetParent( this );

return child;
}

I don't understand the code fragment above but a typical implementation
would be like:

class Child
{
private:
Parent *m_pparent;
public:
Child(Parent (i_pparent) : m_pparent(i_pparent) {}
...
};

class Parent
{
public:
typedef boost::shared_ptr<Child> ChildPtr;
ChildPtr spawn() {return ChildPtr(new Child(this));
...
};

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top