QVector内存泄漏

QVector memory leak?

本文关键字:泄漏 内存 QVector      更新时间:2023-10-16

我之前在这里看到过与此相关的其他问题,但我的问题并不是围绕着包含指针的QVector。

我有一个"Mesh"类,它包含几个QVector<T>,用于保存顶点、索引、法线等,所有这些都作为标准对象保存。

最近我注意到,每次删除网格时,应用程序使用的内存量都不会减少。我一直在用Windows任务管理器监控我的应用程序内存使用情况,并看到它高达1000mb,没有丢失一个字节。

我已经用调试器验证了我正在访问网格类的解构器,并且我的向量正在被删除,但内存仍然没有被释放。

有问题的解构者:

Mesh::~Mesh()
{
    QVector<QVector3D> *vertices = this->vertices;
    QVector<QVector3D> *normals = this->normals;
    QVector<QVector3D> *tangents = this->tangents;
    QVector<QVector2D> *textures = this->UVMap;
    QVector<GLushort> *indices = this->indices;
    vertices->clear();
    vertices->squeeze();
    delete vertices;
    normals->clear();
    normals->squeeze();
    delete normals;
    tangents->clear();
    tangents->squeeze();
    delete tangents;
    textures->clear();
    textures->squeeze();
    delete textures;
    indices->clear();
    indices->squeeze();
    delete indices;
}

我使用过Visual Leak Detector,它似乎主要显示我正在使用的库(Qt)中的泄漏,此外还告诉我我的一些构造函数正在泄漏,如下所示:

Mesh::Mesh()
{
    vertices = new QVector<QVector3D>();
    indices = new QVector<GLushort>();
    UVMap = new QVector<QVector2D>();
    normals = new QVector<QVector3D>();
    tangents = new QVector<QVector3D>();
}

但我没有看到任何问题,因为这些对象在调用之前没有初始化。

我真的还不太了解智能指针,但我很犹豫是否要切换应用程序中的所有内容来使用它们,因为我不知道它们是否能真正或完美地取代我目前的使用,而不会出现任何问题。例如,我可能会向另一个类传递一些指针(例如整个网格),但我可能不希望其他类在网格被破坏时删除该网格。

编辑:我以为我有一个写得很好的问题,但人们似乎更喜欢赶着投票。我将尝试其他程序来监控我的问题。

根本没有任何内存泄漏。以下是valgrind 给出的输出

==7881== Memcheck, a memory error detector
==7881== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7881== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==7881== Command: ./abstractitemmodel
==7881== 
==7881== 
==7881== HEAP SUMMARY:
==7881==     in use at exit: 0 bytes in 0 blocks
==7881==   total heap usage: 8 allocs, 8 frees, 168 bytes allocated
==7881== 
==7881== All heap blocks were freed -- no leaks are possible
==7881== 
==7881== For counts of detected and suppressed errors, rerun with: -v
==7881== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

如果您真的想检查内存泄漏,您应该使用valgrind(适用于linux)等专业软件。valgrind就是一个很好的工具。