cyclic references, shared_ptr, dangling pointer

J

Jarek Blakarz

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;
}
 
F

Fred Zwarts \(KVI\)

"Jarek Blakarz" wrote in message
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;
}
 
J

Jarek Blakarz

int main() {
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.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top