Circular reference-one of the dead points of the smart pointer

Source: Internet
Author: User

The implementation of smart pointers also embodies the C + + object-based principle, the object should be responsible for their own management of resources, including the allocation and release of resources, and the best way to automate the release and allocation of resources, the typical implementation method is to allocate resources in the constructor, the release of resources in the destructor, This way, when other programmers are using this object, the object's resource problem is almost free of extra fuss, which is elegant and convenient.

Then there is something so perfect, and there is no place to overlook it, directly on the code:

Share_ptr.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "Common_class.h" class B;class a{private:typedef tr1::shared_ptr<b> item_type; Public:explicit A () {};virtual ~a () {};p ublic:void Setb (const item_type ptr_b) {m_b = Ptr_b;} Private:item_type M_b;}; Class B{private:typedef tr1::shared_ptr<a> item_type;public:explicit B () {};virtual ~B () {};p ublic:void SetA ( Const Item_type ptr_a) {m_a = ptr_a;} Private:item_type m_a;}; int _tmain (int argc, _tchar* argv[]) {size_t count = 100000;getchar ();//view memory status while (count--) {//new A reference count is now 1shared_ Ptr<a> A (new a);//new the reference count for A is 1shared_ptr<b> B (new B); The reference count for//b is increased to 2A->SETB (B); The reference count for//a is increased to 2b-> SetA (a);} GetChar ();//view memory status//b out scope, B's reference count is reduced to 1, not 0, so B space on the heap is not released,//and B holds no chance to be destroyed, the reference count of a does not reduce the scope of the//a, the reference count of a is reduced to 1, not 0 , so the space on the heap of A is not released return 0;}

Two views view memory resource status Results


The results: Memory increased by almost 20M, not to mention I define the two objects themselves do not account for resources, if the internal maintenance of a few lists, the results imaginable!

A and b all point at each other and yell, "Let go of my quote!" "You send me first, I'll let you go!" , and then the tragedy took place.

So when using smart pointers based on reference counting, be particularly careful about the memory leaks that are caused by circular references, which are not just two-party cases, as long as the reference chain looping is problematic. Of course, the circular reference itself indicates that there may be some problems in the design, if the special reason has to use circular reference, it can let the side of the reference chain with a normal pointer (or the mentally retarded can pointer weak_ptr) can be.

Circular reference-one of the dead points of the smart pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.