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

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


[cpp]  view plain  copy
  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.     }Por_test;  
  13.     QList<Por_test>  slist;  
  14.     for (int i=0;i<100000;i++)  
  15.     {  
  16.         Por_test s;  
  17.         s.id = 1;  
  18.         s.name = QString("hello World!");  
  19.         s.sex = QString("男");  
  20.         slist.append(s);  
  21.     }  
  22.     slist.clear();  
  23.     return a.exec();  
  24. }  

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

[cpp]  view plain  copy
  1. #include <QtCore/QCoreApplication>  
  2. #include <QList>  
  3. #include <QString>  
  4. int main(int argc, char *argv[])  
  5.   
  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 = new Por_test();  
  19.         s->id = 1;  
  20.         s->name = QString("hello World!");  
  21.         s->sex = QString("男?");  
  22.         slist.append(s);  
  23.     }  
  24.     qDeleteAll(slist);  
  25.     slist.clear();  
  26.     return a.exec();  
  27. }  
这里就要在clear()函数前使用qDeleteAll();

from:  http://blog.csdn.net/markely/article/details/8150336

QList内存释放 (收集转载及编辑)


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

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

#include <QtCore/QCoreApplication>#include <QList>#include <QString>
int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    typedef struct _test    {        int id;        QString name;        QString sex;    }Por_test;    QList<Por_test>  slist;    for (int i=0;i<100000;i++)    {        Por_test s;        s.id = 1;        s.name = QString("hello World!");        s.sex = QString("男");        slist.append(s);    }    slist.clear();    return a.exec();}

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

111

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

22222

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

#include <QtCore/QCoreApplication>#include <QList>#include <QString>int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    typedef struct _test    {        int id;        QString name;        QString sex;    }Por_test;    QList<Por_test *>  slist;    for (int i=0;i<100000;i++)    {        Por_test *s = new Por_test();        s->id = 1;        s->name = QString("hello World!");        s->sex = QString("男?");        slist.append(s);    }//    qDeleteAll(slist);    slist.clear();    return a.exec();}

 

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

3333

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

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

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

4444

通过对比靓图,可以看出,内存已经释放,我们再来看下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 方法。


FROM:  http://www.cppblog.com/lauer3912/archive/2011/04/19/144595.aspx?opt=admin



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值