cycles of shared_ptr avoid using weak_ptr.

R

raj s

below lines are from boost"Because shared pointer 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. Use weak_ptr to "break cycles.""
I was trying to create a cycle of smart pointer with the below code
class a{
public:
inline a(){std::cout<<"constructor called";}
shared_ptr<a> class_memeber;

};

void main(){
shared_ptr<a> mainvar(new a);
std::cout<<mainvar.use_count()<<std::endl;
mainvar->class_memeber.reset(mainvar.get());
std::cout<<mainvar->class_memeber;
int i;
std::cin>>i;

}

there is some mistake, can you please explain how the cyclic smart
pointer is created and how to avoid the same with weal_ptr
 
C

Chris Thomasson

raj s said:
below lines are from boost"Because shared pointer 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. Use weak_ptr to "break cycles.""
I was trying to create a cycle of smart pointer with the below code
[...]

there is some mistake, can you please explain how the cyclic smart
pointer is created and how to avoid the same with weal_ptr

I don't use boost shared_ptr, however, AFAICT if you want to create a cycle
you can alter your code to something like:

class a{
public:
shared_ptr<a> class_memeber;
};

int main() {
shared_ptr<a> mainvar(new a);
mainvar->class_memeber = mainvar;
return 0;
}




As for weak_ptr, you can read this and it may help you:

http://www.ddj.com/cpp/184402026
 
R

raj s

below lines are from boost"Because shared pointer 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. Use weak_ptr  to "break cycles.""
I was trying to create a cycle of smart pointer with the below code
[...]

there is some mistake, can you please explain how the cyclic smart
pointer is created and how to avoid the same with weal_ptr

I don't use boost shared_ptr, however, AFAICT if you want to create a cycle
you can alter your code to something like:

class a{
public:
  shared_ptr<a> class_memeber;

};

int main() {
  shared_ptr<a> mainvar(new a);
  mainvar->class_memeber = mainvar;
  return 0;

}

As for weak_ptr, you can read this and it may help you:

http://www.ddj.com/cpp/184402026

from raj post - mainvar->class_memeber.reset(mainvar.get());
from above post mainvar->class_memeber = mainvar;
in both the case the reference count is 1 even after executing the
above statement then how will the memory leak will happen?
 
C

Chris Thomasson

I don't use boost shared_ptr, however, AFAICT if you want to create a
cycle
you can alter your code to something like:
[...]

from raj post - mainvar->class_memeber.reset(mainvar.get());
from above post mainvar->class_memeber = mainvar;
in both the case the reference count is 1 even after executing the
above statement then how will the memory leak will happen?

Humm... The `mainvar' variable constitutes a single reference, when you
assign it to the empty `a::class_member' it should bump the reference count.
Otherwise, it sounds like a bug in shared_ptr; weird.

:^/
 
R

raj s

news:0bc5878c-233b-4300-97cd-7affee5fac3f@p31g2000prf.googlegroups.com.... [...]
there is some mistake, can you please explain how the cyclic smart
pointer is created and how to avoid the same with weal_ptr
I don't use boost shared_ptr, however, AFAICT if you want to create a
cycle
you can alter your code to something like:
[...]

from raj post -    mainvar->class_memeber.reset(mainvar.get());
from above post  mainvar->class_memeber = mainvar;
in both the case the reference count is 1 even after executing the
above statement then how will the memory leak will happen?

Humm... The `mainvar' variable constitutes a single reference, when you
assign it to the empty `a::class_member' it should bump the reference count.
Otherwise, it sounds like a bug in shared_ptr; weird.

:^/

sorry last time i used use_count with out any brace but with brace it
is working fine...
int main() {
shared_ptr<a> mainvar(new a);
cout<<mainvar.use_count;
mainvar->class_memeber = mainvar;
/*return 0; */
cout<<mainvar.use_count();//before mainvar.use_count with out any
braces.
cout<<mainvar->class_memeber.use_count();
int a;
cin>>a;


}
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top