Circular references && workarounds and principles caused by shared_ptr (weak references && strong references)

Source: Internet
Author: User

    weakly Use pointers to resolve shared_ptr caused by circular references to prevent memory leaks!


"* * *" The Circular reference is caused by the smart pointer shared_ptr, the following is the diagram of the use of shared_ptr to cause circular references:

"* * * *" For example, the next shared_ptr caused by the circular reference:

(Background double link list)

<span style= "FONT-SIZE:18PX;" > #include <memory> #include <iostream>using namespace std;struct node{shared_ptr<node> _pre; Shared_ptr<node> _next;~node () {cout << ~node (): "<< this << Endl;} int data;}; void Funtest () {shared_ptr<node> Node1 (new node);shared_ptr<node> Node2 (new node); Node1->_next = Node2; Node2->_pre = Node1;cout << "Node1.use_count:" <<node1.use_count () << endl;cout << " Node2.use_count: "<< node2.use_count () << Endl;} int main () {funtest (); System ("pause"); return 0;} </span>


Execution Result:

(Note: The use of shared_ptr allows a space to have two object management, that is, the first node of the _next domain and the next pointer co-management, or the first pointer and the second node of the _PTR domain jointly managed with its _pcount=2)

A circular reference is the result of the number of objects that appear above because of reference counting and managing space, resulting in the inability of space to release.

We have three solutions for circular references:

"1" when only the last reference is left, you need to manually break the circular reference release object.

2       2" parent children children parent Span style= "font-family:fangsong_gb2312; Font-size:18px ".

3 "3" use a weak reference smart pointer to break this circular reference. Although these three methods are feasible, both method 1 and Method 2 require the programmer to manually control

Error-prone cutting is not easy to operate.

* What are strong and weak references ?  

a strong reference is that when the referenced object is still alive, the reference also exists (that is, if there is at least one strong reference, then the object will not and cannot be freed).

boost::share_ptr is a strong reference.

In contrast, a weak reference does not necessarily exist when the referenced object is alive. is simply a reference when it exists.

A weak reference does not modify the reference count of the object, which means that this weak reference does not manage the memory of the object.

functionally similar to a normal pointer, however, a big difference is that weak references can detect whether the object being managed has been freed, thereby avoiding access to illegal memory R

The appearance of weak_ptr is to assist shared_ptr work, make up for shared_ptr deficiency, solve the problem of circular reference caused by shared_ptr, and this solution of weak_ptr is a weak reference.

#include <memory> #include <iostream>using namespace std;struct node{weak_ptr<node> _pre;weak_ptr <Node> _next;~node () {cout << "~node ():" << this << Endl;} int data;}; void Funtest () {shared_ptr<node> Node1 (new node);shared_ptr<node> Node2 (new node); Node1->_next = Node2; Node2->_pre = Node1;cout << "node1.use_count:" << node1.use_count () << endl;cout << " Node2.use_count: "<< node2.use_count () << Endl;} int main () {funtest (); System ("pause"); return 0;} <


Execution Result:

* from the above results, it is obvious that after using the smart pointer weak_ptr the destructor is called and the space is freed, while the Shead_ptr does not release space.

the "*" must first understand what a smart pointer is before you tell a circular reference:

The following is an analysis of the two pointers shared_ptr and weak_ptr for circular references:

shared_ptr:

shar Ed_ptr complete the work you want : He is responsible for deleting objects pointed to by it (pointee) (pointee)

WEAK_PTR:

 weak_ptris to matchshared_ptrand the introduction of a smart pointer to assistshared_ptrwork, it can be from ashared_ptror anotherweak_ptrobject constructs, and its construction and destruction do not cause an increase or decrease in the number of reference counts. No Overloads*and the -but you can useLockget one of the availableshared_ptrobject.

weak_ptrAn important use ofLockGet Thisof the pointershared_ptr,enable the object to produce itselfshared_ptrto manage themselves, but the helper classEnable_shared_from_thisof theShared_from_thiswill return Thisof theshared_ptr, just want to beshared_ptrmanaged classes inherit from it.



Circular references && workarounds and principles caused by shared_ptr (weak references && strong references)

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.