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>- delete了,析构函数却没有调用
- 毕业了,却没有毕业证...
- delete 指针之后 ,只是释放了指针指向的内存空间,指针不会自动置为NULL,还有delete 遇到指针为NULL时不会调用析构函数
- delete 指针之后 ,只是释放了指针指向的内存空间,指针不会自动置为NULL,还有delete 遇到指针为NULL时不会调用析构函数
- Android studio 里面方法明明被调用了,却显示没有被调用!
- 析构函数里对指针成员调用delete
- new、delete对调用 析构函数 的影响
- 文件没有下载却被扣分了
- 大学还在,读书人却没有了
- delete析构的调用
- 微信支付异步回调的坑,调用成功了回调地址却没有数据
- 重载new和delete后调用构造函数和析构函数顺序
- 重载new和delete后调用构造函数和析构函数顺序
- 毕业了,找到工作了,但却没有兴奋感!
- c++如果定义了析构函数,则必须有析构函数的函数体,那怕是空的也行,如果没有定义析构函数,则会调用默认的析构函数
- Effective C++条款6:析构函数里对指针成员调用delete
- 条款6:析构函数里对指针成员调用delete
- effective C++笔记之条款6:析构函数里对指针成员调用delete
- [珠玑之椟]二分思想与分治法、排序思想
- Hdu 4009 Transfer water (最小树形图)
- 小聊自动化测试工具和框架
- C++注册表操作
- boost一些测试
- delete了,析构函数却没有调用
- css实现按钮透明色
- c语言中的rand()函数和srand()函数产生随机的整数
- Oracle 11g 客户端的安装和配置。
- C#中几种数据类型转化总结
- linux下串口编程VTIME和VMIN参数的用法
- SharePoint 2013 高亮显示当前导航菜单
- dom4j解析xml以及dom4j生成xml详解
- 砖2 虚拟机网络配置