首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt5.3( QQuickView )中删除QQuickView时的内存管理问题

Qt5.3( QQuickView )中删除QQuickView时的内存管理问题
EN

Stack Overflow用户
提问于 2014-11-29 11:49:37
回答 1查看 1.6K关注 0票数 2

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

通过向应用程序添加一个QWidget,将分配大约30 be的内存,但当小部件删除时,将只释放大约20 be的内存。

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

而且,我很确定QQuickView没有正确地释放内存。

如何释放QQuickView分配的整个内存?

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

提前谢谢..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-29 15:32:42

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

代码语言:javascript
复制
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中,而是在您自己的代码中。

此外,值得一提的是,任务管理器实际显示的进程使用的内存比GetProcessMemoryInfo()少,我认为后者是这两者的更准确的衡量标准。任务管理器读取也没有显示任何内存泄漏,尽管其值波动较大。

这是输出:

代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27201876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档