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

2014-11-23 23:21:19 · 作者: · 浏览: 12

析构函数在对象的生命结束时,会自动调用,大家所熟知的智能指针就是根据析构函数的这种特性而实现的,包括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++的第(3)条规范相悖,下面我以一个简单的示例来说明:

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


[html]
testa.h文件
#ifndef _TEST_A_H
#define _TEST_A_H
class CTestA {
public:
CTestA();
~CTestA();
};
#endif

testa.h文件
#ifndef _TEST_A_H
#define _TEST_A_H
class CTestA {
public:
CTestA();
~CTestA();
};
#endif
[html]
testa.cpp文件

#include "testa.h"
#include
CTestA::CTestA()
{

}

CTestA::~CTestA()
{
qDebug() << "~CTestA()";
}

testa.cpp文件

#include "testa.h"
#include
CTestA::CTestA()
{

}

CTestA::~CTestA()
{
qDebug() << "~CTestA()";
}

[html]
testb.h文件

#ifndef _TEST_B_H
#define _TEST_B_H
class CTestA;
class CTestB {
public:
static CTestB *getInstance();
CTestA *getTestA();

private:
CTestB();
~CTestB();

private:
CTestA *pTestA;
static CTestB mSelf;
};
#endif

testb.h文件

#ifndef _TEST_B_H
#define _TEST_B_H
class CTestA;
class CTestB {
public:
static CTestB *getInstance();
CTestA *getTestA();

private:
CTestB();
~CTestB();

private:
CTestA *pTestA;
static CTestB mSelf;
};
#endif
[html]
testb.cpp文件
#include "testb.h"
#include "testa.h"
#include
CTestA::CTestA()
{

}

CTestA::~CTestA()
{
qDebug() << "~CTestA()";
}

testb.cpp文件
#include "testb.h"
#include "testa.h"
#include
CTestA::CTestA()
{

}

CTestA::~CTestA()
{
qDebug() << "~CTestA()";
}

[html]
testapp.h文件

#ifndef _TEST_APP_H
#define _TEST_APP_H
class CTestA;
class CTestApp {
public:
CTestApp(CTestA *pTestA);
~CTestApp();
private:
CTestA *pTestA;
};
#endif

testapp.h文件

#ifndef _TEST_APP_H
#define _TEST_APP_H
class CTestA;
class CTestApp {
public:
CTestApp(CTestA *pTestA);
~CTestApp();
private:
CTestA *pTestA;
};
#endif
[html]
testapp.cpp文件

#include "testapp.h"

testapp.cpp文件

#include "testapp.h"[html] view plaincopyprint #include

#include [html] view plaincopyprint //#include "testa.h"

//#include "testa.h"[html] view plaincopyprint CTestApp::CTestApp(CTestA *pTestA)
{
t