Why does QApplication give memory leaks?

1.5k views Asked by At

I have a simple code:

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    return 0;
}

I compile it in Qt Creator using pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testLeaks
TEMPLATE = app


SOURCES += main.cpp

HEADERS  +=

FORMS    +=

Command valgrind ./testLeaks says about leaks and prints the following:

==31276== HEAP SUMMARY:
==31276==     in use at exit: 1,190,544 bytes in 7,267 blocks
==31276==   total heap usage: 46,096 allocs, 38,829 frees, 6,716,079 bytes allocated
==31276== 
==31276== LEAK SUMMARY:
==31276==    definitely lost: 2,788 bytes in 11 blocks
==31276==    indirectly lost: 7,065 bytes in 182 blocks
==31276==      possibly lost: 318,238 bytes in 1,233 blocks
==31276==    still reachable: 862,453 bytes in 5,841 blocks
==31276==         suppressed: 0 bytes in 0 blocks

If I comment QApplication, there are no leaks. Why does this class give leaks?

1

There are 1 answers

1
László Papp On BEST ANSWER

As noted in the comments, the following command provides more information:

valgrind --leak-check=full --show-leak-kinds=all -v ./testLeaks

Also, you cut out the end of the short output:

==3005== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2).

Either way, it is very likely that it is not a bug in QApplication itself, but some underlying dependency, especially you mentioned that it would happen after a libc update.

I have had several problems with libc and other low-level library where you would think they do not leak memory, and you are surprised in the end of the day.

This could be easily checked by writing an example using that library.

However, since it is just about 320K+ and Qt applications (especially GUI) will require a lot more, I would not consider this as a non-negligible amount.