Valgrind shows 23.5% memory leak for Qt lib on x86 platform

  • I compiled QT-4.7.2 source on x86 platform and wrote a sample application to display a small window with a quit button. The program exits normally when Quit button is clicked. I then ran Valgrind tool to detect memory leaks which shows significantly large memory leaks (about 23.5%) for such a small application.

    @[e518816@pe1800xs button]$ valgrind --leak-check=full --track-origins=yes --log-file=valgrind.log -v ./button@

    @==2340== HEAP SUMMARY:
    ==2340== in use at exit: 1,152,738 bytes in 9,640 blocks
    ==2340== total heap usage: 32,752 allocs, 23,112 frees, 4,918,688 bytes allocated
    ==2340== Searching for pointers to 9,640 not-freed blocks
    ==2340== Checked 10,218,524 bytes

    @==2340== LEAK SUMMARY:
    ==2340== definitely lost: 2,956 bytes in 11 blocks
    ==2340== indirectly lost: 241,467 bytes in 1,391 blocks
    ==2340== possibly lost: 428,024 bytes in 2,777 blocks
    ==2340== still reachable: 480,291 bytes in 5,461 blocks
    ==2340== suppressed: 0 bytes in 0 blocks
    ==2340== Reachable blocks (those to which a pointer was found) are not shown.
    ==2340== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==2340== ERROR SUMMARY: 1487 errors from 1362 contexts (suppressed: 5 from 1)@

    The qt application "source code": and full "valgrind log": file can be downloaded. Please help to resolve this memory leak issue as I want to port this application on a ARM9 based Freescale iMX target board with limited RAM.

    Thanks & Regards,
    Manavendra Nath Manav

  • What you should do, is make sure you delete all pointers you declare before the application closes (*quit, *desktop, *widget).

  • Moderators

    Nah, meta object system should take care of that.

    Are you using separate Valgrind instance or this built-in in Qt Creator? I've heard there are some known false-positives popping up in valgrind, when checking any Qt app. The built-in version is "aware" of those pitfalls and does not report them.

    EDIT: havent tried that myself, though, so you may treat this post as gossiping ;)

  • I ran Valgrind instance with build-in Qt creator. The valgrind report again shows memory leak even with the suppressions file for Qt47 included. The build-in Qt creator actually uses the same /usr/bin/valgrind executable, the only difference being the use of suppressions file which restricts some leaks and conditions check from popping up in the report. The full log file is attached "here":

  • After analyzing the Valgrind log, the functions causing the maximum memory leaks multiple times are:

    QFontDatabase::load() in libQtGui
    qHBNewFace() in libQtCore
    g_type_create_instance() in libgobject
    FT_Load_Glyph() in libfreetype
    XML_ParseBuffer() in libexpat

    In the above list, the functions q_type_create_instance() and qHBNewFace() has been called most of the times. From the attached valgrind log it appers that remaining memory leaks are result of functions using g_strdup and g_memdup which are not freed.

    Is my analysis in the right direction? Or, am I missing something here?

    Thanks & Regards,
    Manavendra Nath Manav

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.