You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt程序被检测到内存泄漏,是否为误报?

Qt Hello World程序AddressSanitizer检测到libfontconfig内存泄漏是否为误报?

问题详情

我在Debian Stable系统上使用系统Qt库,以及源码安装的GCC 12.2编译以下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();
}

编译命令如下:

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

关闭程序时,AddressSanitizer检测到内存泄漏:

$ ./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).

请问这是否属于误报?

回答

这属于误报,原因如下:

  • 泄漏的内存来自系统的libfontconfig.so.1库,而非你编写的Qt代码。fontconfig在初始化时会分配一些全局缓存内存,这些内存会保留到进程结束——因为进程退出时操作系统会自动回收所有内存,这种设计是合理的,不属于真正的内存泄漏。
  • Qt启动时会间接调用fontconfig的接口完成字体相关初始化,这些全局内存不会在Qt应用关闭时被fontconfig主动释放,因此被AddressSanitizer检测到,但实际没有内存占用风险。

若想消除这类误报,可参考两种方法:

  • 添加ASAN泄漏排除规则:创建fontconfig.supp文件,内容为
    leak:libfontconfig.so.1
    
    运行程序时指定环境变量:LSAN_OPTIONS=suppressions=fontconfig.supp ./a.out
  • 直接忽略来自系统库的这类全局内存泄漏报告,它们不会引发实际问题。

内容的提问来源于stack exchange,提问作者MWB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:47:37