在Qt5.3(mingw32)中删除QQuickView的内存管理问题 [英] Memory management issue with deleting QQuickView in Qt5.3(mingw32)

查看:232
本文介绍了在Qt5.3(mingw32)中删除QQuickView的内存管理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are developing an application with Qt/Qml (Qml is embedded in the QWidget). When deleting a QWidget that contains a QQuickView (embedded), the allocated memory won't be freed entirely.

通过向应用程序添加QWidget,将分配大约30MB的内存,但是当该小部件删除时,将仅释放大约20MB的内存.

By adding a QWidget to application, about 30MB of memory will be allocated but when the widget deletes, only about 20MB of memory will be freed.

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

In the destructor of QWidget, I have deleted the QQuickView instance and there are no other big objects.

此外,我非常确定QQuickView不会正确释放内存.

Also, I am pretty sure that the QQuickView doesn't release memory properly.

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

How can I free entire memory allocated by QQuickView?

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

NOTE: The code is really big (160000 Lines), therefore I can't put sample code.

预先感谢...

推荐答案

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

I've written a quick test to determine if there is an actual leak in the creation and deletion of a 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;
};

它具有一个计时器,该计时器创建/销毁了一个内部装有已加载QML文件的QQuickWidget,尽管结果最初有所提高,但内存使用情况随时间稳定下来,这表明Qt中不太可能发生内存泄漏代码,并且如果您确实泄漏了内存,则故障不在于Qt,而在于您自己的代码.

It has a timer that creates/destroys a QQuickWidget with a loaded QML file inside, and although the results initially ramp up, the memory usage stabilizes in time, indicating it is not likely there was a memory leak in the Qt code, and that if you indeed leak memory, the fault does not lie in Qt but in your own code.

此外,值得一提的是,任务管理器实际显示的进程使用的内存少于GetProcessMemoryInfo(),并且我认为后者是两者的更准确度量.任务管理器读数也没有显示任何内存泄漏,尽管其值波动更大.

Also, it is worth mentioning that the task manager actually showed the process using less memory than GetProcessMemoryInfo(), and I think the latter is the more accurate measure of the two. The task manager reading also didn't indicate any memory leaking although its value fluctuated more.

以下是输出:

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

这篇关于在Qt5.3(mingw32)中删除QQuickView的内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆