qDeleteAll 之后必须清空容器
【1】qDeleteAll应用示例
qDeleteAll源码如下:
template <typename ForwardIterator>
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
{
while (begin != end)
{
delete *begin;
++begin;
}
} template <typename Container>
inline void qDeleteAll(const Container &c)
{
qDeleteAll(c.begin(), c.end());
}
示例代码如下:
#include <QMap>
#include <QString>
#include <QWidget>
#include <QDebug>
#include <QApplication> class PersonInfo
{
public:
PersonInfo(int id = -, QString name = QString(), QWidget* pWidget = NULL)
: m_nId(id)
, m_name(name)
, m_pWidget(pWidget)
{} ~PersonInfo()
{
m_nId = -;
m_name = QString();
if (m_pWidget != NULL)
{
qDebug() << "delete personInfo :: " << this;
delete m_pWidget;
m_pWidget = NULL;
}
}
private:
int m_nId;
QString m_name;
QWidget* m_pWidget;
}; int main(int argc, char *argv[])
{
QApplication a(argc, argv); QMap<int, PersonInfo *> map;
for (int i = ; i < ; ++i)
{
PersonInfo *pTemp = new PersonInfo(i + , QString("hello World!"), (new QWidget()));
qDebug() << "construct personInf :: " << pTemp;
map.insert(i, pTemp);
} qDeleteAll(map); // 1.释放内存
qDebug() << "after qDeleteAll size :: " << map.size() << endl;
map.clear(); // 2.必须清空
qDeleteAll(map); // 3.再次释放内存 return a.exec();
}
打印结果如下:
construct personInf :: 0x3bdc20
construct personInf :: 0x3c0308
construct personInf :: 0x3bfe08
construct personInf :: 0x3c0a88
construct personInf :: 0x3c1248
construct personInf :: 0x3c1738
construct personInf :: 0x3c1bb8
construct personInf :: 0x3c2420
construct personInf :: 0x3c26d0
construct personInf :: 0x3c2c68
delete personInfo :: 0x3bdc20
delete personInfo :: 0x3c0308
delete personInfo :: 0x3bfe08
delete personInfo :: 0x3c0a88
delete personInfo :: 0x3c1248
delete personInfo :: 0x3c1738
delete personInfo :: 0x3c1bb8
delete personInfo :: 0x3c2420
delete personInfo :: 0x3c26d0
delete personInfo :: 0x3c2c68
after qDeleteAll size ::
尤其注意打印结果。
可以尝试把2.清空容器行注释掉,然后再运行程序,查看运行结果。
【2】总结
qDeleteAll只负责释放容器元素内存,但没有对容器的置空操作。若无意间二次再执行qDeleteAll过程,程序必会崩溃。
为了避免可恶的崩溃,切记qDeleteAll之后必须清空容器。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
qDeleteAll 之后必须清空容器的更多相关文章
- 用AS3清空容器下所有子显示对象
容器中的子显示对象分为两类: 处于显示列表中的子显示对象.被numChildren所记录的. 由容器graphics对象绘制出来的矢量图.这个矢量图不属于Shape类型,不在容器的显示列表中,不被nu ...
- QT 随笔目录
[1]基础部分 <信号和槽机制> <信号与槽知识点> <QString 与 string转换> <QT 继承QWidget && 继承QDia ...
- Qt中的通用模板算法QtAlgorithms(qDeleteAll,qBinaryFind,qCountLeadingZeroBits,qPopulationCount,qFill,qSwap,qSort)
Qt在<QtAlgorithms>头文件中为我们提供了一系列的全局模板方法,这些模板方法主要用于容器操作,比如qDeleteAll().其在Qt中的声明如下: void qDeleteAl ...
- Spring容器深入(li)
spring中最常用的控制反转和面向切面编程. 一.IOC IoC(Inversion of Control,控制倒转).对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的 ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- JAVA容器
JAVA容器 一.容器体系结构 java.util 二.迭代器Iterator<E> 迭代器是一种设计模式,可以遍历并选择序列中的对象,而开发人员并不需要了解该序列的底层结构.迭代器通常被 ...
- STL标准模板库 向量容器(vector)
向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...
- [翻译] C++ STL容器参考手册(第二章 <deque>)
返回总册 本章节原文:http://www.cplusplus.com/reference/deque/deque/ 1. std::deque template < class T, clas ...
随机推荐
- ES6新特性三: Generator(生成器)函数详解
本文实例讲述了ES6新特性三: Generator(生成器)函数.分享给大家供大家参考,具体如下: 1. 简介 ① 理解:可以把它理解成一个函数的内部状态的遍历器,每调用一次,函数的内部状态发生一次改 ...
- vuex是什么?怎么使用?哪种功能场景使用它?
vuex是vue框架中状态管理.在main.js引入store,注入.新建了一个目录store,...export.应用场景有:单页应用中,组件之间的状态.应用实例:音乐播放.登录状态.加入购物车等等
- MyBatis传递参数
MyBatis传递参数 一.使用 map 接口传递参数 在 MyBatis 中允许 map 接口通过键值对传递多个参数,把接口方法定义为 : public List<Role> findR ...
- 检测到目标URL存在http host头攻击漏洞
检测到目标URL存在http host头攻击漏洞 1.引发安全问题的原因 为了方便的获得网站域名,开发人员一般依赖于HTTP Host header.例如,在php里用_SERVER["HT ...
- php 7 新特性整理小结
php 7 比php 5 性能提升了很多,php 7 新特性主要表现在:1.变量存储字节减小,减少内存占用,提升变量操作速度:2.改善数组结构,数组元素和hash映射表被分配在同一块内存里,降低了内存 ...
- AsyncStorage和Promise配合使用
代码: AsyncStorage封装 import {AsyncStorage} from "react-native"; class DeviceStorage { //保存数据 ...
- shell文件查找和压缩命令
locate命令 1.locate filename 模糊匹配 只要文件名中包含关键字即可 非实时查找,速度比find快. 依靠索引数据库 /var/lib/mlocate/m ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- selenium PO模式
思想: 1.定义basepage.py用来写公共方法,比如找元素,打开url,切换frame.这样的部分都写在这里.不必每次用都重写. 2.LoginPage.py 每个功能模块一个文件或者一个类 这 ...
- 独立出properties的mybatis连接池
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/java505?useSSL=true&chara ...