一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

QList<T> 的释放分两种情况:

1.T的类型为非指针,这时候直接调用clear()方法就可以释放了,看如下测试代码

复制代码
 1 #include <QtCore/QCoreApplication>
 2 #include <QList>
 3 #include <QString>
 4 
 5 int main(int argc, char *argv[])
 6 {    
 7     QCoreApplication a(argc, argv);   
 8      typedef struct _test    
 9     {        
10         int id;        
11         QString name;        
12         QString sex;    
13     }
14     Por_test;    
15     QList<Por_test>  slist;    
16     for (int i=0;i<100000;i++)    
17     {        
18         Por_test s;        
19         s.id = 1;        
20         s.name = QString("hello World!");        
21         s.sex = QString("");        
22         slist.append(s);    
23     }    
24     slist.clear();   
25     return a.exec();
26 }   
复制代码

将上面代码中的slist.clear(); 注释掉,内存显示为如下(任务管理器里的截图)

 如不去掉的话,内存显示如下图

 2.T的类型为指针的情况,这时候直接调用clear()方法将不能释放,先看代码

复制代码
 1 #include <QtCore/QCoreApplication>
 2 #include <QList>
 3 #include <QString>
 4 int main(int argc, char *argv[])
 5 {    
 6     QCoreApplication a(argc, argv);    
 7     typedef struct _test    
 8     {       
 9          int id;        
10         QString name;        
11         QString sex;    
12     }
13     Por_test;    
14     QList<Por_test *>  slist;    
15     for (int i=0;i<100000;i++)    
16     {        
17         Por_test *s = new Por_test();        
18         s->id = 1;        
19         s->name = QString("hello World!");        
20         s->sex = QString("男?");        
21         slist.append(s);    
22     }//    
23     qDeleteAll(slist);   
24     slist.clear();    
25     return a.exec();
26 }
复制代码

上面代码运行后的内存情况如下图

说明当T的类型为指针时,调用clear()方法并不能释放其内存

此时void qDeleteAll ( const Container & c )方法将派上用场了,将上面代码中的注释去掉以后,

再次运行程序,此时的内存情况如下图

通过对比靓图,可以看出,内存已经释放,我们再来看下qt助手中qDeleteAll 方法的说明

void qDeleteAll ( ForwardIterator begin, ForwardIterator end )

Deletes all the items in the range [beginend) using the C++ delete operator. The item type must be a pointer type (for example, QWidget *).

Example:

QList<Employee *> list; 
list.append(new Employee("Blackpool", "Stephen"));
list.append(new Employee("Twist", "Oliver"));
qDeleteAll(list.begin(), list.end());
list.clear();

Notice that qDeleteAll() doesn't remove the items from the container; it merely calls delete on them. In the example above, we call clear() on the container to remove the items.

This function can also be used to delete items stored in associative containers, such as QMap and QHash. Only the objects stored in each container will be deleted by this function; objects used as keys will not be deleted.

See also forward iterators.

void qDeleteAll ( const Container & c )

This is an overloaded member function, provided for convenience.

This is the same as qDeleteAll(c.begin(), c.end()).

 

上面qDeleteAll 方法的说明,已经很清楚了,如果T为指针类型时,释放内存须在clear方法前加上qDeleteAll 方法。

 

posted on   一杯清酒邀明月  阅读(1034)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2021-01-27 OpenCV 轮廓查找与绘制-最小外接矩形
2021-01-27 OpenCV 提取轮廓的凸包、外包矩形、最小外包矩形、最小外包圆
2021-01-27 OpenCV boundingRect、minAreaRect的用法区别
2021-01-27 OpenCV findContours函数参数详解
2021-01-27 OpenCV 画直线函数 line()
2021-01-27 OpenCV circle() 画圆函数
2021-01-27 OpenCV approxPolyDP 多边拟合函数
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示