Delete, the destructor does not call

Source: Internet
Author: User

The destructor is automatically invoked at the end of the object's life, and the familiar smart pointers are implemented according to the characteristics of the destructor, including the memory management mechanism of QT, which is implemented by the mechanism of destructor. C + + founder Bjarne Stroustrup in the creation of destructors is also for this purpose, it can be seen that if the destructor with good words, you could save us a lot of work, you no longer need to manually call the deleted object to use the heap memory, you just need to delete the heap into the destructor in the row, Because the destructor is called automatically when the object leaves its life cycle, the C + + language specification prescribes the invocation of the destructor:

Destructors is invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program Terminatio N (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) while the block in which the object is Crea Ted Exits (6.7), (3) for a constructed temporary object when the lifetime of the temporary object Ends (12.2), (4) for a C Onstructed object allocated by a newexpression (5.3.4), through with a deleteexpression (5.3.5), (5) in several situatio NS due to the handling of exceptions (15.3). A program was illformed if an object of class type or array thereof are declared and the destructor for the class are not ACC Essible at the point of the declaration. Destructors can also be invoked explicitly.

The effect is:

Destructors can be implicitly called in the following five ways:

(1) An object constructed in a static store (also known as a global object or a static object, which is placed in the data area of the program), and when the program ends, the object's destructor is automatically called.

(2) in an automatic storage area (also known as a local object, which is placed in the program's stack), the object's constructor is automatically invoked when the object being constructed leaves its area, such as an object declared within 1 functions, leaving the scope of the function.

(3) An object that is temporarily constructed when it leaves its life cycle, the destructor of the object is called, and (2).

(4) The object of the new construct (that is, the object is in the heap), deleted by Delete, the destructor is called

(5) In the case of try,catch handling exceptions, when the object in the try block, because of an exception, enters the catch branch, the constructor is called in the Catch branch

The above 5 are generated by the compiler by default call method, of course, there are 1 kinds of can be called by the way of the display, that is, like calling a function called destructors

With the above introduction, the next step into our subject, delete, but do not call the destructor of the case, this is the above mentioned in the C + + (4) specification, the following is a simple example to illustrate:

Example consists of 7 files, Testa.cpp,testa.h, testb.cpp,testb.h,testapp.h,testapp.cpp,main.cpp

[HTML]View PlainCopy
    1. Testa.h file
    2. #ifndef _test_a_h
    3. #define _test_a_h
    4. Class Ctesta {
    5. Public
    6. Ctesta ();
    7. ~ctesta ();
    8. };
    9. #endif

[HTML]View PlainCopy
    1. Testa.cpp file
    2. #include "Testa.h"
    3. #include <qdebug.h>
    4. Ctesta::ctesta ()
    5. {
    6. }
    7. Ctesta::~ctesta ()
    8. {
    9. Qdebug () << "~ctesta ()";
    10. }

[HTML]View PlainCopy
    1. testb.h file   
    2.   
    3. # ifndef _test_b_h  
    4. #define &NBSP;_TEST_B_H&NBSP;&NBSP;
    5. class  ctesta;  
    6. class ctestb {  
    7. PUBLIC:&NBSP;&NBSP;
    8.     static ctestb *getinstance ();   
    9.     ctesta *gettesta ();   
    10.   
    11. PRIVATE:&NBSP;&NBSP;
    12.     ctestb ();   
    13.     ~ctestb ();   
    14.   
    15. PRIVATE:&NBSP;&NBSP;
    16.     ctesta *ptesta;  
    17. &NBSP;&NBSP;&NBSP;&NBSP;STATIC&NBSP;CTESTB&NBSP;MSELF;&NBSP;&NBSP;
    18. };  
    19. #endif   

[HTML]View PlainCopy
  1. Testb.cpp file
  2. #include "Testa.h"
  3. Ctestb ctestb::mself;
  4. Ctestb *ctestb::getinstance ()
  5. {
  6. Return &mSelf;
  7. }
  8. CTESTB::CTESTB ()
  9. {
  10. Ptesta = new Ctesta ();
  11. }
  12. CTESTB::~CTESTB ()
  13. {
  14. Delete Ptesta;
  15. Qdebug () << "~CTESTB ()";
  16. }
  17. Ctesta *ctestb::gettesta ()
  18. {
  19. return ptesta;
  20. }

[HTML]View PlainCopy
    1. Testapp.h file
    2. #ifndef _test_app_h
    3. #define _test_app_h
    4. Class Ctesta;
    5. Class Ctestapp {
    6. Public
    7. Ctestapp (Ctesta *ptesta);
    8. ~ctestapp ();
    9. Private
    10. Ctesta *ptesta;
    11. };
    12. #endif

[HTML]View PlainCopy
  1. Testapp.cpp file
  2. #include "Testapp.h"
  3. #include <qdebug.h>
  4. #include "Testa.h"
  5. Ctestapp::ctestapp (Ctesta *ptesta)
  6. {
  7. this->ptesta = Ptesta;
  8. }
  9. Ctestapp::~ctestapp ()
  10. {
  11. Delete Ptesta;
  12. Qdebug () << "~ctestapp ()";
  13. }

[HTML]View PlainCopy
    1. Main.cpp file
    2. #include "Testb.h"
    3. #include "Testcpp.h"
    4. int main (int argc, char *argv[])
    5. {
    6. Qapplication app (argc, argv);
    7. CTESTB *ptestb = ctestb::getinstance ();
    8. Ctestapp *Ptestapp = new Ctestapp (ptestb->gettesta ());
    9. Delete Ptestapp;
    10. return App.exec ();
    11. }

Here are the output results,

~ctestapp ()

Description Delete Ptesta, after not called the Ptesta destructor, when the Testapp.cpp file//#include "testa.h" comment cancellation, delete will call Ptesta destructor, which is the output after the comment is removed:

~ctesta ()

~ctestapp ()

Note above the compilation environment is Ming32-make, do not know the other compilation environment will not appear so problem, this left to everyone to verify.

Here is the disassembly code, which adds the testa.h delete Ptesta disassembly code to the Testapp.cpp:

Delete Ptesta;
0x0040255c  <+8>:            mov    0x8 (%EBP),%eax
0x0040255f  <+11>:            mov    (%eax),%EBX
0x00402561  <+13>:            test   %EBX,%EBX
0x00402563  <+15>:            JE     0x402575 <~CTestApp+33>
0x00402565  <+17>:            mov    %ebx, (%ESP)
0x00402568  <+20>:            call   0x403082 <~CTestA>
0x0040256d  <+25>:            mov    %ebx, (%ESP)
0x00402570  <+28>:            call   0x40aa68 <_ZdlPv>

This is the delete Ptesta disassembly code without testa.h in Testapp.cpp:

Delete Ptesta;
0x00402550  <+8>:            mov    0x8 (%EBP),%eax
0x00402553  <+11>:            mov    (%eax),%eax
0x00402555  <+13>:            mov    %eax, (%ESP)
0x00402558  <+16>:            call   0x40aa48 <_ZdlPv>

You can see the testa.h in the disassembly, called the destructor ~ctesta, call 0x403082 <~CTestA>

http://blog.csdn.net/rabinsong/article/details/9347615

Delete, the destructor does not call

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.