One test test (C++11) The crash conclusion of a smart pointer reference

Source: Internet
Author: User

The smart pointer is used in the project, but to get the shared_ptr pointer of this class to the class it refers to in the class that the smart pointer points to, there is a problem with the result.

The test code is as follows: (including error interpretation)

1 //test shared_ptr weak_ptr map<string,shared_ptr>2#include <stdio.h>//pinrtf ...3#include <string>//string ...4#include <map>5#include <memory>//shared_ptr ...6 7 classC;8 9 classA {Ten  Public: OneA () {printf ("A () \ n"); } A~a () {printf ("~a () \ n"); } -  -STD::stringA; theStd::weak_ptr<c>with ; -     VirtualSTD::stringGeta () =0; - }; -  + classA1: PublicA { -  Public: +A1 (std::stringAA) {a = AA; printf ("A1 (%s) \ n", Aa.c_str ()); }; A~A1 () {printf ("~a1 (%s) \ n", A.c_str ()); } at  -STD::stringGeta () {std::stringb ="A1"+ A;returnb;} - }; -  - classA2: PublicA { -  Public: inA2 (std::stringAA) {a = AA; printf ("A2 (%s) \ n", Aa.c_str ()); }; -~a2 () {printf ("~a2 () \ n"); printf"~a2 (%s) \ n", A.c_str ()); } to  +STD::stringGeta () {std::stringb ="A2"+ A;returnb;} - }; the  *typedef std::shared_ptr<a>Shra; $typedef std::shared_ptr<a1>shrA1;Panax Notoginsengtypedef std::shared_ptr<a2>shrA2; -  thetemplate<class_ty> + classAA { A  Public: theAA () {printf ("AA () \ n"); } +~aa () {printf ("~aa () \ n"); } -  $typedef STD::MAP&LT;STD::string, _ty>Atype; $ atype list; - }; -  the  - classCWuyi { the  Public: -C () {printf ("C () \ n"); } Wu~c () {printf ("~c () \ n"); }//In this destructor the breakpoint will be found to enter two times, two times the  destructor - Shra H1, H2; AboutStd::weak_ptr<a>A; $  -     voidAdd (Shra h) { -         if(H1 = =nullptr) { -H1 =h; A             //std::shared_ptr<c> p = (std::shared_ptr<c>) this;//This method does not release memory, but the reference count is incorrect (here is weak_ptr, but the shared_ptr count is also incorrect) +H->with = std::shared_ptr<c> ( This);//This will result in the creation of temporary objects (including construction and destruction, which will release the memory of this) during assignment. the                                                 //because it is written in this way, the shared_ptr is generated directly here, so the reference count becomes 1, causing the memory to be freed when the destructor is broken . -                                                 //The external reference pointer points to the freed memory,,, so, Crash ~ ~ $         } the         Else if(H2 = =nullptr) { theH2 =h; theH->with = std::shared_ptr<c> ( This);//Ibid . the         } -printf"C::add%s\n",h->Geta (). C_STR ()); in     } the }; thetypedef std::shared_ptr<c>SHRC; About  the classCC the { the  Public : +CC () {printf ("CC () \ n"); } -~CC () {printf ("~cc () \ n"); } the Bayitypedef STD::MAP&LT;STD::string, std::shared_ptr<c>>Ctype; the Ctype list; the }; -  - intMain () { the     { theAa<shra1>Aa1; theAa<shra2>Aa2; the  - cc CC; the  theShrA1 A1 = ShrA1 (NewA1 ("Ah")); theAuto B = Aa1.list.emplace (Std::make_pair ("A1", A1));94  theShrA1 A11 = shrA1 (NewA1 ("Oh , yes.")); theb = Aa1.list.emplace (Std::make_pair ("A1", A11));//key Repeat, placement failed (B.seond==false) the 98ShrA2 A2 (NewA2 ("portion of Rice")); AboutAuto B2 = Aa2.list.emplace (Std::make_pair ("A2", a2)); - 101printf"\ n-------------\ n");102          for(auto p:aa1.list)103printf"%s\n", p.second->Geta (). data ());104printf"\ n-------------\ n"); theSHRC C (NewC ());106Cc.list.emplace ("C1", c);107         //C->add (A11); C->add A1/A11/A2 will cause a crash .108C->Add (A2);109     } the     return 0;111}

Conclusion:

In the process of assignment, use a shape such as

Std::shared_ptr<c> (This)

The shared_ptr pointer to this pointer will generate a new shared_ptr pointer,,, instead of the pointer you want (the pointer defined outside the class),

Although the address seems to be the same (the debugging process to see the next), but in fact is not the same only pointer,

It can be seen from a different count.

In the same way, other similar constructs new objects.

But even if it's written like this,

Std::shared_ptr<c> p = (std::shared_ptr<c>) this;

Forcing the type to be converted is also a new smart pointer pointer object.

As a matter of fact

Std::shared_ptr<c> (This)

itself is a statement that constructs a new object

One test test (C++11) The crash conclusion of a smart pointer reference

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.