Smart pointers - passing 'this' around

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonzo

    Smart pointers - passing 'this' around

    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<Contai nerNode>
    ContainerNode :: CreateChildCont ainer()
    {
    SmartPtr<Contai nerNode> child( new ContainerNode );

    // a) This would cause 'this' to be deallocated at end of scope!
    // SmartPtr<Contai nerNode> 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<Contai nerNode> parent;
    SmartPtr<Contai nerCode> child;
    ....
    child = parent->CreateChildCon tainer();
    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
  • Cy Edmunds

    #2
    Re: Smart pointers - passing 'this' around

    "Bonzo" <dont@email.m e> wrote in message
    news:dont-B5B60F.21005522 072003@syrcnyrd rs-02-ge0.nyroc.rr.co m...[color=blue]
    > 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?[/color]

    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.)
    [color=blue]
    >
    > Here's where the problem occurs:
    >
    > SmartPtr<Contai nerNode>
    > ContainerNode :: CreateChildCont ainer()
    > {
    > SmartPtr<Contai nerNode> child( new ContainerNode );
    >
    > // a) This would cause 'this' to be deallocated at end of scope!
    > // SmartPtr<Contai nerNode> smart( this );
    > // child->SetParent( smart );
    >
    > // b) Can't assign raw ptr to WeakPtr in SetParent.
    > // child->SetParent( this );
    >
    > return child;
    > }[/color]

    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_ppa rent) {}
    ...
    };

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

    <snip>

    --
    Cy



    Comment

    Working...