这是一个几乎最小的Qt程序,应该释放所有资源,包括内存:
#include <QApplication>
#include <QMainWindow>
#include <memory>
int main(int argc, char** argv) {
QApplication app(argc, argv);
std::unique_ptr<QWidget> wnd{new QWidget()};
wnd->show();
return app.exec();
}
然而,
valgrind
指出:LEAK SUMMARY:
definitely lost: 979 bytes in 24 blocks
indirectly lost: 7,858 bytes in 56 blocks
possibly lost: 912 bytes in 19 blocks
still reachable: 75,719 bytes in 1,080 blocks
of which reachable via heuristic:
newarray : 832 bytes in 16 blocks
suppressed: 0 bytes in 0 blocks
Rerun with --leak-check=full to see details of leaked memory
我本以为会得到“绝对丢失”和“间接丢失”的零值,但却获得了丢失的字节。为什么呢?是我错误地解释了valgrind
的输出,还是我需要调用Qt的某些额外退出函数?
QObject
。像所有其他使用 PIMPL 的 Qt 对象一样,QWidget
的大小为void*
。通过值来持有它。 - Kuba hasn't forgotten Monica