Search code examples
c++linuxqtgccaddress-sanitizer

Does Qt leak memory?


If I compile this 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();
}

on Debian Stable (using system Qt libraries) with GCC 12.2 (installed from source)

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

This leads to leaks being detected, when I close the application:

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

Is this a false positive?


Solution

  • I underestand that in QtCreator we can add CONFIG += sanitizer sanitize_address in the .pro File to enable sanitizer and I test in a simple template project with one MainWindow. I test it in Qt 5.15.2 and I saw a memory leaks error.

    enter image description here

    But as I test it in Qt_6_2_3 it works correctly. So I think this relates to Qt 5 version and they fixed it in Qt 6.

    enter image description here