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

Advertisements

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

Advertisements

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.
 

Top