Dead Point of smart pointer-circular reference

Source: Internet
Author: User

In C ++'s latest standard C ++ 11, the intelligent pointer share_prt based on reference counting is included in the bag. The threshold for using smart pointers is getting lower and lower, and boost libraries are not required, we can also easily enjoy the convenience that smart pointers bring to us.

Smart pointer, like its name, seems to be a perfect smart role,ProgramMembers no longer need to worry about the release of new ones. For example, when a resource is shared by multiple modules, the programmer needs to trigger the release of the pointer when the lifecycle of all modules ends, the module life cycle may be written at all.CodeCannot be determined.

The emergence of smart pointers has brought a glimmer of light to C ++ that does not support the garbage collection mechanism. The following describes the operating mechanism of smart pointers:

When we need to apply for space from the stack, we can hand over the new pointer to the smart pointer management, such as shared_ptr <int> A (New INT);, so that when a is out of scope, when object A is destructed, the heap pointer held by object A is released, which is implemented through the C ++ destructor.

When a copy value of a smart pointer object is assigned to another smart pointer, for example, shared_ptr <int> B = A; A and B point to the space on the same stack, when any object in A or B is out of scope, the space on the stack should not be released, because another smart pointer object is referencing the heap space, so we introduced the reference counter mechanism to solve this problem. When a smart pointer object is created, a space for counting is created on the stack. After shared_ptr <int> B = A; is executed, object B Copies the pointer of the counting area of object A, and then adds the value of the counting area to 1. In this way, a group of smart pointers assigned by the copy operation point to the data space on the same heap and share the other heap counting zone (this is also the reason for shared_ptr ). In the analysis of the smart pointer object, instead of simply releasing the heap Data Space held, the shared reference count is-1, and then the reference count is 0, to call Delete.

The implementation of smart pointers also embodies the object-based principle of C ++. objects should be responsible for their managed resources, including resource allocation and release, in addition, it is best to automate the release and allocation of resources. A typical implementation method is to allocate resources in the constructor and release resources in the destructor, in this way, when other programmers use this object, there is almost no extra worry about the resource issues of this object, that is, elegance and convenience.

 

Well, I am a gorgeous splitting line. The focus of this article is as follows. When a circular reference occurs, the count-based sharing mechanism will be completely defeated.

For a simple example, see note: (the following code is run. memory usage will soar due to memory leakage. Please be careful when you test it. Do not crash your computer)

 
Class B; Class A {public: shared_ptr <B> m_ B ;}; Class B {public: shared_ptr <A> M_a ;}; int main () {While (true) {shared_ptr <A> A (new A); // The reference count of the new A is 1 shared_ptr <B> B (New B ); // The reference count of New B is 1 A-> m_ B = B; // The reference count of B is increased to 2 B-> M_a =; // A's reference count is increased to 2} // B's first out-of-scope, B's reference count is reduced to 1, not 0, so the B space on the stack is not released, in addition, the held by B has no chance to be destructed, and the reference count of A has not been reduced at all // A is out of scope. Similarly, the reference count of A is reduced to 1, not 0, so the space on the stack a is not released}

In this way, both A and B point to each other and yell, "Let go of my reference! "," I will let you go when you first send me !", So the tragedy happened.

 

when using a smart pointer based on the reference count, pay special attention to the memory leakage caused by circular references. Circular references are not just about two sides, as long as the reference chain is made into a ring, problems will occur. Of course, the circular reference itself indicates that there may be some problems in the design. If you have to use circular reference for special reasons, you can use a common pointer (or weak smart pointer weak_ptr) for one party on the reference chain) you can.

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.