Memory management issue with deleting QQuickView in Qt5.3(mingw32)(在 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 的内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Qt5.3(mingw32) 中删除 QQuickView 的内存管理问题
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01