cycles of shared_ptr avoid using weak_ptr.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raj s

    cycles of shared_ptr avoid using weak_ptr.

    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<< "constructo r called";}
    shared_ptr<acla ss_memeber;

    };

    void main(){
    shared_ptr<amai nvar(new a);
    std::cout<<main var.use_count() <<std::endl;
    mainvar->class_memeber. reset(mainvar.g et());
    std::cout<<main var->class_memebe r;
    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
Working...