Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   cyclic references, shared_ptr, dangling pointer (http://www.velocityreviews.com/forums/t954220-cyclic-references-shared_ptr-dangling-pointer.html)

Jarek Blakarz 11-05-2012 03:45 PM

cyclic references, shared_ptr, dangling pointer
 
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;
}

Fred Zwarts \(KVI\) 11-05-2012 03:57 PM

Re: cyclic references, shared_ptr, dangling pointer
 
"Jarek Blakarz" wrote in message
news:1d365c49-d8ba-4385-9699-f5becaddb63a@googlegroups.com...
>
>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;
}


Jarek Blakarz 11-06-2012 10:44 AM

Re: cyclic references, shared_ptr, dangling pointer
 
> >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.


All times are GMT. The time now is 10:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.