|
Boost Users : |
Subject: Re: [Boost-users] newbie question on cyclic shared pointers
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-09-07 00:06:13
On Sat, Sep 6, 2008 at 21:48, MGE <mick.elliot_at_[hidden]> wrote:
>
> So I am trying to manually break the cycle at deletion. I would be grateful
> if somebody could take a look at the code below, which illustrates my basic
> approach. Assuming that I stringently break all cycles when a Child class or
> Parent class goes out of scope, would this approach avoid memory leaks?
> Would it be possible to put code into the destructor of Parent and Child
> classes that automatically sets the shared pointer owned by the class to
> null (not using the reset() function but by using the method below) hence
> guaranteeing that cycles are broken at deletion?
>
You probably don't want to assign them to a new shared_ptr, since the
allocation is spurious.
This is sufficient in the example:
// later, manually break the cycle
child->myParent.reset();
In your real case, you may need to reset others, of course.
HTH,
~ Scott
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net