问题描述
我们正在使用 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
:
它有一个计时器,可以创建/销毁一个带有加载的 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.
这是输出:
这篇关于在 Qt5.3(mingw32) 中删除 QQuickView 的内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!