delete了,析构函数却没有调用

来源:互联网 发布:巴西日本人知乎 编辑:程序博客网 时间:2024/04/29 18:28

析构函数在对象的生命结束时,会自动调用,大家所熟知的智能指针就是根据析构函数的这种特性而实现的,包括Qt的内存管理机制,也都是利用了析构函数的这一机制来实现的。c++创始人Bjarne Stroustrup在创造析构函数也是出于这种目的的,可见如果析构函数用的好的话,可以省去我们很多工作量,你不再需要手工调用删除对象使用的堆内存,你只需要把要删除的堆内存放入析构函数就行了,因为当对象离开其生命周期的时候,析构函数会自动调用,C++语言规范是这样规定析构函数的调用的:

Destructors are invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program termination (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) when the block in which the object is created exits (6.7), (3) for a constructed temporary object when the lifetime of the temporary object ends (12.2), (4) for a constructed object allocated by a newexpression (5.3.4), through use of a deleteexpression (5.3.5), (5) in several situations due to the handling of exceptions (15.3). A program is illformed if an object of class type or array thereof is declared and the destructor for the class is not accessible at the point of the declaration. Destructors can also be invoked explicitly.

大意是:

析构函数可以由以下五种方式隐含调用:

(1)在静态存储区(也即全局对象或静态对象,这个对象放在程序的数据区)里面构造的对象,当程序结束时,对象的析构函数会自动调用。

(2)在自动存储区(也即局部对象,这个对象放在程序的栈里)里面构造的对象离开其区域时,如1个函数内声明的对象,离开函数作用域时,对象的构造函数会自动调用。

(3)临时构造的对象当离开其生命周期时,该对象的析构函数会调用,此处同(2)。

(4)new构造的对象(即对象在堆区),通过delete删除,析构会调用

(5)在try,catch处理异常的情况下,当在try块中对象,因为异常,进入catch分支,会在catch分支中调用构造函数

以上5种是通过编译器生成的默认调用方式,当然了,还有1种就是可以通过显示的方式调用,也即像调用函数一样调用析构函数

有了上面的介绍,接下来进入我们的主题, delete了,却不调用析构函数的情况,这与上面说的C++的第(4)条规范相悖,下面我以一个简单的示例来说明:

示例由7个文件组成,testa.cpp,testa.h, testb.cpp,testb.h,testapp.h,testapp.cpp,main.cpp

testa.h文件#ifndef _TEST_A_H#define _TEST_A_Hclass CTestA {public:    CTestA();    ~CTestA();};#endif

testa.cpp文件#include "testa.h"#include <qdebug.h>CTestA::CTestA(){}CTestA::~CTestA(){    qDebug() << "~CTestA()";}

testb.h文件#ifndef _TEST_B_H#define _TEST_B_Hclass CTestA;class CTestB {public:    static CTestB *getInstance();    CTestA *getTestA();private:    CTestB();    ~CTestB();private:    CTestA *pTestA;    static CTestB mSelf;};#endif

testb.cpp文件#include "testa.h"CTestB CTestB::mSelf;CTestB *CTestB::getInstance(){    return &mSelf;}CTestB::CTestB(){    pTestA = new CTestA();}CTestB::~CTestB(){    delete pTestA;    qDebug() << "~CTestB()";}CTestA *CTestB::getTestA(){    return pTestA;}

testapp.h文件#ifndef _TEST_APP_H#define _TEST_APP_Hclass CTestA;class CTestApp {public:    CTestApp(CTestA *pTestA);    ~CTestApp();private:    CTestA *pTestA;};#endif

testapp.cpp文件#include "testapp.h"#include <qdebug.h>//#include "testa.h"CTestApp::CTestApp(CTestA *pTestA){    this->pTestA = pTestA;}CTestApp::~CTestApp(){    delete pTestA;    qDebug() << "~CTestApp()";}

main.cpp文件#include "testb.h"#include "testcpp.h"int main(int argc, char *argv[]){    QApplication app(argc, argv);    CTestB *pTestB = CTestB::getInstance();    CTestApp *pTestApp = new CTestApp(pTestB->getTestA());    delete pTestApp;    return app.exec();}
下面是输出结果,

~CTestApp() 

说明delete pTestA;后没有调用pTestA的析构函数,当把testapp.cpp文件的//#include "testa.h"注释取消,delete后会调用pTestA析构函数,这是去掉注释后的输出结果:

~CTestA()

~CTestApp() 

注以上的编译环境是ming32-make,不知道其他的编译环境会不会出现如此问题,这个留给大家去验证。

下面是反汇编代码,这是在testapp.cpp里面加了testa.h的delete pTestA反汇编代码:

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>

这是在testapp.cpp里面没有加testa.h的delete pTestA反汇编代码:

delete pTestA;
0x00402550  <+8>:            mov    0x8(%ebp),%eax
0x00402553  <+11>:            mov    (%eax),%eax
0x00402555  <+13>:            mov    %eax,(%esp)
0x00402558  <+16>:            call   0x40aa48 <_ZdlPv>
可以看到加了testa.h的反汇编中,调用了析构函数~CTestA, call 0x403082 <~CTestA>