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
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