C++ 删除Qt5.3(mingw32)中的QQuickView时出现内存管理问题

C++ 删除Qt5.3(mingw32)中的QQuickView时出现内存管理问题,c++,qt,memory-leaks,qml,qquickview,C++,Qt,Memory Leaks,Qml,Qquickview,我们正在用Qt/Qml开发一个应用程序(Qml嵌入到QWidget中)。删除包含QQuickView(嵌入式)的QWidget时,分配的内存不会完全释放 通过向应用程序添加一个QWidget,将分配大约30MB的内存,但当widget删除时,只会释放大约20MB的内存 在QWidget的析构函数中,我删除了QQuickView实例,没有其他大对象 另外,我很确定QQuickView不能正确释放内存 如何释放QQuickView分配的全部内存 注意:代码非常大(160000行),因此我无法放置示例

我们正在用Qt/Qml开发一个应用程序(Qml嵌入到QWidget中)。删除包含QQuickView(嵌入式)的QWidget时,分配的内存不会完全释放

通过向应用程序添加一个QWidget,将分配大约30MB的内存,但当widget删除时,只会释放大约20MB的内存

在QWidget的析构函数中,我删除了QQuickView实例,没有其他大对象

另外,我很确定QQuickView不能正确释放内存

如何释放QQuickView分配的全部内存

注意:代码非常大(160000行),因此我无法放置示例代码


提前感谢…

我编写了一个快速测试,以确定在创建和删除
QQUickWidget
时是否存在实际泄漏:

class Widget : public QWidget {
    Q_OBJECT
public:
    Widget(QWidget *parent = 0) : QWidget(parent) {
        widget = 0;
        count = 0;
        resize(200, 200);
        layout = new QVBoxLayout(this);
        setLayout(layout);
        QTimer * t = new QTimer(this);
        t->setInterval(200);
        t->setSingleShot(false);
        t->start();
        connect (t, SIGNAL(timeout()), this, SLOT(toggleQuickView()));
    }

public slots:
    void toggleQuickView() {
        if (!widget) {
            widget = new QQuickWidget;
            widget->setSource(QUrl::fromLocalFile("d:\\main.qml"));
            connect(widget, SIGNAL(destroyed()), this, SLOT(echo()));
            layout->addWidget(widget);
        } else {
            layout->removeWidget(widget);
            widget->deleteLater();
            widget = 0;
        }
    }

    void echo() {
        PROCESS_MEMORY_COUNTERS memcount;
        if (!GetProcessMemoryInfo(GetCurrentProcess(), &memcount, sizeof(memcount))) return;
        qDebug() << ++count << "created and destroyed," << memcount.WorkingSetSize / (1024 * 1024) << "MB memory used";
    }

private:
    QVBoxLayout * layout;
    QQuickWidget * widget;
    int count;
};

当添加QWidget时,您是如何测量的?30MB被分配,而仅释放20MB?如果您的程序具有基类派生类,请确保将基类析构函数声明为虚拟的。这确保了与该特定对象相关的所有类都能适当地释放资源。@MantoshKumar通过在“Windows任务管理器”中检查它。这不是很可靠,重要的是,它不能直接从程序堆内存映射。它基本上讲述了私有字节的总使用量,其增加和减少取决于许多因素。我建议您阅读此链接以获得有关此内容的完整信息:@Mosi-创建并销毁此小部件10次,然后查看是否有100 mb“泄漏”。
1 created and destroyed, 41 MB memory used
2 created and destroyed, 44 MB memory used
3 created and destroyed, 44 MB memory used
4 created and destroyed, 48 MB memory used
5 created and destroyed, 48 MB memory used
6 created and destroyed, 48 MB memory used
7 created and destroyed, 48 MB memory used
8 created and destroyed, 48 MB memory used
9 created and destroyed, 48 MB memory used
10 created and destroyed, 48 MB memory used
11 created and destroyed, 52 MB memory used
12 created and destroyed, 52 MB memory used
13 created and destroyed, 52 MB memory used
14 created and destroyed, 52 MB memory used
15 created and destroyed, 52 MB memory used
16 created and destroyed, 52 MB memory used
17 created and destroyed, 52 MB memory used
18 created and destroyed, 52 MB memory used
19 created and destroyed, 52 MB memory used
20 created and destroyed, 52 MB memory used
21 created and destroyed, 53 MB memory used
...
50 created and destroyed, 53 MB memory used
...
100 created and destroyed, 53 MB memory used
...
200 created and destroyed, 53 MB memory used
...
500 created and destroyed, 53 MB memory used