cyclic references, shared_ptr, dangling pointer

Discussion in 'C++' started by Jarek Blakarz, Nov 5, 2012.

  1. Hi
    I can read the following in the official documentation of boost shared_ptr:
    "Because the implementation uses reference counting, cycles of shared_ptr
    instances will not be reclaimed. For example, if main() holds a shared_ptr to
    A, which directly or indirectly holds a shared_ptr back to A, A's use count
    will be 2. Destruction of the original shared_ptr will leave A dangling with a
    use count of 1."

    I wrote the following example that in fact causes memory leak and not a
    dangling pointer. Please provide me with an example of dangling pointer
    described in the official boost documentation.

    thanks.

    struct A {
    shared_ptr < A > a;
    };

    int main() {
    shared_ptr<A> a (new A);
    a->a = a;

    return 0;
    }
    Jarek Blakarz, Nov 5, 2012
    #1
    1. Advertising

  2. "Jarek Blakarz" wrote in message
    news:...
    >
    >Hi
    >I can read the following in the official documentation of boost shared_ptr:
    >"Because the implementation uses reference counting, cycles of shared_ptr
    >instances will not be reclaimed. For example, if main() holds a shared_ptr
    >to
    >A, which directly or indirectly holds a shared_ptr back to A, A's use count
    >will be 2. Destruction of the original shared_ptr will leave A dangling
    >with a
    >use count of 1."
    >
    >I wrote the following example that in fact causes memory leak and not a
    >dangling pointer.


    Where did you read that you could have a dangling pointer?
    The quote above speaks about "... leave A dangling ...",
    that is something different.

    > Please provide me with an example of dangling pointer
    >described in the official boost documentation.
    >
    >thanks.
    >
    >struct A {
    > shared_ptr < A > a;
    >};
    >
    >int main() {
    > shared_ptr<A> a (new A);
    > a->a = a;
    >
    > return 0;
    >}


    int main() {

    {
    shared_ptr<A> a (new A);
    a->a = a;
    }

    // There is no pointer, but the object previously accessible using "a" is
    now dangling, i.e., inaccessible.

    return 0;
    }
    Fred Zwarts \(KVI\), Nov 5, 2012
    #2
    1. Advertising

  3. > >int main() {
    >
    > > shared_ptr<A> a (new A);

    >
    > > a->a = a;

    >
    > >

    >
    > > return 0;

    >
    > >}

    >
    >
    >
    > int main() {
    >
    >
    >
    > {
    >
    > shared_ptr<A> a (new A);
    >
    > a->a = a;
    >
    > }
    >
    >
    >
    > // There is no pointer, but the object previously accessible using "a" is
    >
    > now dangling, i.e., inaccessible.


    Right. Looks like I misunderstood it. Thanks for explanation. Now everything is clear to me.
    Jarek Blakarz, Nov 6, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    11
    Views:
    590
    Sharad Kala
    Nov 5, 2004
  2. Tony Johansson

    dangling references?

    Tony Johansson, Apr 6, 2005, in forum: C++
    Replies:
    6
    Views:
    541
    Alf P. Steinbach
    Apr 7, 2005
  3. Richard

    Detecting dangling memory references

    Richard, May 3, 2004, in forum: C Programming
    Replies:
    5
    Views:
    324
    Peter Nilsson
    May 4, 2004
  4. Matthias Kaeppler

    Dangling references?

    Matthias Kaeppler, Oct 10, 2005, in forum: C++
    Replies:
    6
    Views:
    426
  5. Colin Caughie
    Replies:
    1
    Views:
    696
    Shooting
    Aug 29, 2006
Loading...

Share This Page