C + + Shared_from_this data search

Source: Internet
Author: User

Information about the Shared_from_this search:

1. Tcpconnection used the Enable_shared_from_this base class, which provides a shared_from_this () common method that allows the child class to get inside the shared_ptr object. Used in the class implementation process where you need to pass the pointer itself. There are a few points to note:

    • Although Enable_shared_from_this is a base class, it does initialize the enable_shared_from_this member weak_ptr inside the shared_ptr. Therefore, it is not possible to call Shared_from_this () in the constructor of a subclass , because weak_ptr is still null at this time.
    • Why does the this pointer not be used directly inside the class, because our program uses shared_ptr to manage pointers, and if we use raw pointers in the process of passing within the class, such references within the class shared_ptr not be aware of, Because it was possible that we had been released by shared_ptr when we passed in.

2. shared_ptr Enable_shared_from_this

one way to avoid a memory leak is always use a named smart pointer variable to hold the result of new

Shared_ptr<t> p (new T);

The boost document begins with the Enable_shared_from_this role: the header <boost/enable_shared_from_this.hpp> defines the Class template Enable_shared_from_this. It is used as a base class This allows a shared_ptr to the current object to being obtained from within a member function.

    • Enable_shared_from_this<d> as the base class for D, so that D inherits its two public functions:

Shared_ptr<d> shared_from_this(); Const shared_ptr<d> shared_from_this const ();

    • The D object itself is not directly called shared_from_this (), and in the boost code design, D inherits from the private member of Enable_shared_from_this<d> Weak_ptr<d > Weak_this_ is initialized from a third-party function call D._internal_accept_owner (shared_ptr<d> const * DPTR, D * PD), and this third-party function is in share Implemented in D_PTR.HPP and called by the Shared_ptr<> constructor (it is easy to pass this over)
1#include <boost/shared_ptr.hpp>2#include <boost/enable_shared_from_this.hpp>3#include <iostream>4 5 using namespacestd;6 using namespaceboost;7 8 classWY: PublicEnable_shared_from_this<wy>{9  Public:TenWY (inti): I_ (i) {cout <<"WY ' s constructor"<<Endl;} One  A     intI () {returnI_;} -      -Shared_ptr<wy>get_shared_ptr () { thecout <<"In get_shared_ptr ==> i ="<< I_ <<Endl; -         returnshared_from_this (); -     } - Private : +     intI_; - }; +  A intMain () at { -WY WY (6);//this WY object actually has its members (inherited from Enable_shared_from_this<wy>) Weak_ptr<wy>, and has been initialized, so calling Wy.shared_from_this () will throw the wrong  -Shared_ptr<wy> PTR (NewXyg5));//This ptr holds the WY, its weak_ptr<wy> -shared_ptr<wy> p = ptr->shared_from_this (); -Ptr->get_shared_ptr (); - wy.get_shared_ptr (); inp =wy.shared_from_this (); -}

The weak_ptr in the 24-row WY is not initialized, and 25 lines of PTR are initialized

Program to 28 lines when the print out in get_shared_ptr i = 6 after the abnormal exit, that is, as long as the call Shared_from_this program with WY is an exception exit

The source code for the 2nd in the above analysis is for 1-46-one said, when I look at the previous version of the source code, I found that this is not the implementation of this function, so we just need to remember when the boost for the external use of the way, that is to Shared_ptr<d> PD is held in D, where the weak_this_ is initialized before it can be called shared_from_this, in such a way pd->shared_from_this ()

3. Shared_from_this () uses the Shared_from_this function to obtain a shared_ptr that points to itself in a class where it is necessary to pass the class object itself shared_ptr, which is enable_shared_from_ The member function of the this<t>, which returns shared_ptr<t>.
The first thing to note is that this function can only be used after the Shared_ptr<t> constructor is called. The reason is that enable_shared_from_this::weak_ptr is not set in the Enable_shared_from_this<t> constructor, but is set in the constructor of Shared_ptr<t>.

A) The following code is wrong:

1 class D: public boost::enable_shared_from_this<d>  2  {  3   Public:  4     D ()  5  {  6          boost::shared_ptr<d> p=shared_from_this ();   7    }  8 };  

The reason is simple, although in the constructor of D it is guaranteed that the Enable_shared_from_this<d> constructor has been called, but as previously stated, WEAK_PTR has not been set.
b) The following code is also wrong:

1 classD: PublicBoost::enable_shared_from_this<d>2  {  3   Public:  4      voidfunc ()5      {  6Boost::shared_ptr<d> p=Shared_from_this (); 7      }  8  }; 9  voidMain ()Ten  {   One d D;  A D.func ();  -}

The cause of the error is ibid.
c) The following code is correct:

 1  void   main ()  2   { 3   boost::shared_ptr<d> D (new   D);   4  d->func ();  5 } 

Here boost::shared_ptr<d> D (new D) actually performs 3 actions:
1. First call the Enable_shared_from_this<d> constructor;
2. Second, call the constructor of D;
3. Finally call the Shared_ptr<d> constructor.
The 3rd action sets the enable_shared_from_this<d> weak_ptr, not the 1th action . This place is very contrary to C + + commonsense and logic and must be careful.

The conclusion is: Do not use shared_from_this in constructors, and second, if you want to use shared_ptr, you should use it everywhere, you cannot use D D, and you must never pass a bare pointer.

C + + Shared_from_this data search

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.