QTextCodec相关的new、delete问题一则
来源:互联网 发布:win10 修改intel mac 编辑:程序博客网 时间:2024/05/29 16:14
在 Qt之QTextCodec乱谈 一文中我们提到这个一样例子
int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QTextCodec * codec = new DbzhangCodec; QTextCodec::setCodecForCStrings(codec); qDebug()<<QString("hello world!"); return 0;}
其中,DbzhangCodec是QTextCodec的一个派生类。这儿使用了 new ,但是却没有相应地使用 delete,可是却没有造成内存泄漏,原因何在?
构造与析构函数
从构造函数开始看起:当对象被创建时,它会将指向自己的指针添加到一个static的列表中。
/*! Constructs a QTextCodec, and gives it the highest precedence. The QTextCodec should always be constructed on the heap (i.e. with \c new). Qt takes ownership and will delete it when the application terminates.*/QTextCodec::QTextCodec(){... all->prepend(this);}
其中:all是一个static的列表
static QList<QTextCodec*> *all = 0;
看一下析构函数
/*! Destroys the QTextCodec. Note that you should not delete codecs yourself: once created they become Qt's responsibility.*/QTextCodec::~QTextCodec(){ if (all) { all->removeAll(this);... }}
上面的两段注释明确告诉我们:对象必须分配在heap中,而且由Qt负责在程序退出之前删除它。
可是,删除操作是怎么实现的呢?
QTextCodecCleanup
class QTextCodecCleanup{public: ~QTextCodecCleanup();};/* Deletes all the created codecs. This destructor is called just before exiting to delete any QTextCodec objects that may be lying around.*/QTextCodecCleanup::~QTextCodecCleanup(){... for (QList<QTextCodec *>::const_iterator it = all->constBegin() ; it != all->constEnd(); ++it) { delete *it; } delete all; all = 0;...}
这是一个很简单的类,只定义了一个析构函数:负责删除列表 all 中的所有QTextCodec对象。
要使得这个析构函数被调用,显然需要构造一个QTextCodecCleanup对象,这是通过:
Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
实现的
Q_GLOBAL_STATIC
这个宏定义在 qglobal.h 这个头文件内(根据你所用的Qt的版本不同,你的源码初看起来与此可能有很大不同)。
#define Q_GLOBAL_STATIC(TYPE, NAME) \ static TYPE *NAME() \ { \ static QGlobalStatic<TYPE > thisGlobalStatic \ = { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \ if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) { \ TYPE *x = new TYPE; \ if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) \ delete x; \ else \ static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \ } \ return thisGlobalStatic.pointer; \ }
由于考虑了线程安全性,这个宏看起来还真乱,其实呢,也就是一个静态的函数
static QTextCodecCleanup * createQTextCodecCleanup(){}
该函数内,创建了两个静态的对象
static QGlobalStatic<QTextCodecCleanup> thisGlobalStatic;static QGlobalStaticDeleter<QTextCodecCleanup> cleanup(thisGlobalStatic);
thisGlobalStatic 中保存有 QTextCodecCleanup 对象的指针(并作为函数的返回值返回);cleanup 析构时将析构掉该指针所指向的对象。
- QTextCodec相关的new、delete问题一则
- QTextCodec相关的new、delete问题一则
- QTextCodec相关的new、delete问题一则 .
- new delete的问题
- 除虫一则--关于new []/delete[]的误用
- C++ new delete的问题
- C++ new和delete的相关形式
- Vector的new与delete问题
- c++中new和delete,delete[]的匹配问题(转载)
- c++ new new[]和 delete delete[] 问题
- new delete问题
- malloc/free 和 new/delete相关的面试题
- C++ new、delete(new[]、delete[])操作符重载需要注意的问题
- new/delete 的用法
- new, delete 的用法
- C ++的new、delete
- 关于operator new及placement new,和相关的delete重载
- 关于c++中new和delete的长度问题
- SQL 2005:备份集中的数据备份与现有数据库不同
- sqlserver2000 孤立用户
- [MFC]RichEdit响应回车换行,自动水平/垂直滚动
- C++复习
- magento -- 推荐插件 --Ajax更新购物车数量 --Ajax Shopping Cart Quantity Update
- QTextCodec相关的new、delete问题一则
- http://edu.codepub.com/2010/1108/27051.php
- 张永民老师选课介绍
- (转)架构风格与基于网络的软件架构设计(介绍REST)
- 关于动态CSS载入
- sql 判断 一个字段是否为数字
- C#的get set 可访问性
- MyEclipse 添加junit包
- C_basis