Qt会泄漏内存吗?

4
如果我编译这个Qt的“hello world”:
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>


int main( int argc, char **argv ) {
  QApplication a( argc, argv );

  QPushButton hello( "Hello world!" );
  hello.resize( 100, 30 );

  hello.show();
  return a.exec();
}

在Debian Stable上(使用系统Qt库),使用从源代码安装的GCC 12.2

$ g++ -fsanitize=address hello.cpp -L /usr/lib/x86_64-linux-gnu -lQt5Core -lQt5Gui -lQt5Widgets -I /usr/include/x86_64-linux-gnu/qt5 -fPIC

这会导致在我关闭应用程序时检测到泄漏:
$ ./a.out 

=================================================================
==3563910==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6656 byte(s) in 26 object(s) allocated from:
    #0 0x7fea3b4801af in __interceptor_malloc ../../../../gcc-12.2.0/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x7fea3693c704  (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x20704)

Indirect leak of 1600 byte(s) in 50 object(s) allocated from:
    #0 0x7fea3b47fb97 in __interceptor_calloc ../../../../gcc-12.2.0/libsanitizer/asan/asan_malloc_linux.cpp:77
    #1 0x7fea3693cd48  (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x20d48)

Indirect leak of 519 byte(s) in 50 object(s) allocated from:
    #0 0x7fea3b437f3b in __interceptor_strdup ../../../../gcc-12.2.0/libsanitizer/asan/asan_interceptors.cpp:439
    #1 0x7fea3693bfa4 in FcValueSave (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x1ffa4)

SUMMARY: AddressSanitizer: 8775 byte(s) leaked in 126 allocation(s).

这是一个误报吗?


可能是由于QT的特殊内存管理系统。在销毁时,父对象会删除挂起的子对象。因此,QT中使用new创建的小部件不应该被显式地delete。但这只是一种猜测。 - Red.Wave
3
很难说,泄漏似乎与Qt无关,而是与libfontconfig有关。 https://github.com/behdad/fontconfig/blob/master/README 中谈到了很多“修复泄漏”的内容。 - Öö Tiib
1
请注意,此泄漏报告的调用堆栈不包含 main 函数。这意味着静态链接的动态库 libfontconfig.so 存在问题。请注意,其他人在不使用 Qt 的情况下也遇到了相同的问题:https://github.com/Raymo111/i3lock-color/issues/88 - Marek R
1个回答

2
我了解在QtCreator中,我们可以在.pro文件中添加CONFIG += sanitizer sanitize_address来启用sanitizer。我在一个只有一个MainWindow的简单模板项目中进行了测试。我在Qt 5.15.2中进行了测试,并发现了内存泄漏错误。

enter image description here

但是当我在Qt_6_2_3中测试它时,它可以正常工作。 因此,我认为这与Qt 5版本有关,并且他们在Qt 6中修复了它。

enter image description here


1
这个问题在Qt 5.15.8中似乎已经被修复了。 - MWB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接