在 Qt5.3(mingw32) 中删除 QQuickView 的内存管理问题
Memory management issue with deleting QQuickView in Qt5.3(mingw32)
我们正在开发一个带有Qt/Qml的应用程序(Qml嵌入在QWidget中)。删除包含 QQuickView(嵌入式)的 QWidget 时,分配的内存不会完全释放。
通过将QWidget添加到应用程序,将分配大约30MB的内存,但是当小部件删除时,只会释放大约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;
};
它有一个计时器,可以创建/销毁一个QQuickWidget
,里面有一个加载的QML文件,虽然结果最初会增加,但内存使用量会及时稳定下来,这表明Qt代码中不太可能存在内存泄漏,如果你确实泄漏了内存,错误不在于Qt,而在于你自己的代码。
另外,值得一提的是,任务管理器实际上使用比GetProcessMemoryInfo()
更少的内存显示了该过程,我认为后者是两者中更准确的度量。任务管理器读数也没有表明任何内存泄漏,尽管其值波动更大。
这是输出:
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
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 在 Qt5.3(mingw32) 中删除 QQuickView 的内存管理问题