Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-127706

Memory leaks even in the simplest Qt programs

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.5.3
    • QPA: X11/XCB
    • None

    Description

      Consider this very basic program:

      main.cpp:

      #include <QApplication>
      #include <QtWidgets>
      
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
      
          QLabel label("Hello World!");
          label.show();
      
          return app.exec();
      }
      

      untitled.pro:

      QT += widgets
      
      CONFIG += c++17
      
      SOURCES += main.cpp
      

      If I build this with debug on Ubuntu and then run valgrind --num-callers=500 --leak-check=full ./untitled, I get a memory leak:

      ==161281== Memcheck, a memory error detector
      ==161281== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
      ==161281== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
      ==161281== Command: ./untitled
      ==161281==
      qt.qpa.plugin: Could not load the Qt platform plugin "wayland" in "" even though it was found.
      ==161281==
      ==161281== HEAP SUMMARY:
      ==161281==     in use at exit: 96,433 bytes in 961 blocks
      ==161281==   total heap usage: 15,756 allocs, 14,795 frees, 3,535,593 bytes allocated
      ==161281==
      ==161281== 352 bytes in 1 blocks are possibly lost in loss record 114 of 141
      ==161281==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==161281==    by 0x40147D9: calloc (rtld-malloc.h:44)
      ==161281==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
      ==161281==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
      ==161281==    by 0x590F7B4: allocate_stack (allocatestack.c:430)
      ==161281==    by 0x590F7B4: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
      ==161281==    by 0x52671D4: QThread::start(QThread::Priority) (in /Qt/6.5.3/gcc_64/lib/libQt6Core.so.6.5.3)
      ==161281==    by 0x8C195EB: QDBusConnection::sessionBus() (in /Qt/6.5.3/gcc_64/lib/libQt6DBus.so.6.5.3)
      ==161281==    by 0x6152DFC: QGenericUnixServices::QGenericUnixServices() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x97DBB09: QXcbIntegration::QXcbIntegration(QList<QString> const&, int&, char**) (in /Qt/6.5.3/gcc_64/lib/libQt6XcbQpa.so.6.5.3)
      ==161281==    by 0x977C3EE: ??? (in /Qt/6.5.3/gcc_64/plugins/platforms/libqxcb.so)
      ==161281==    by 0x5C5A86E: ??? (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x5C5E2CB: QGuiApplicationPrivate::createPlatformIntegration() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x5C5ECEF: QGuiApplicationPrivate::createEventDispatcher() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x50D8645: QCoreApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Core.so.6.5.3)
      ==161281==    by 0x5C61FDF: QGuiApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x49E0D48: QApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Widgets.so.6.5.3)
      ==161281==    by 0x109353: main (main.cpp:6)
      ==161281==
      ==161281== 456 (24 direct, 432 indirect) bytes in 1 blocks are definitely lost in loss record 116 of 141
      ==161281==    at 0x4849013: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==161281==    by 0x6152E21: QGenericUnixServices::QGenericUnixServices() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x97DBB09: QXcbIntegration::QXcbIntegration(QList<QString> const&, int&, char**) (in /Qt/6.5.3/gcc_64/lib/libQt6XcbQpa.so.6.5.3)
      ==161281==    by 0x977C3EE: ??? (in /Qt/6.5.3/gcc_64/plugins/platforms/libqxcb.so)
      ==161281==    by 0x5C5A86E: ??? (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x5C5E2CB: QGuiApplicationPrivate::createPlatformIntegration() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x5C5ECEF: QGuiApplicationPrivate::createEventDispatcher() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x50D8645: QCoreApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Core.so.6.5.3)
      ==161281==    by 0x5C61FDF: QGuiApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Gui.so.6.5.3)
      ==161281==    by 0x49E0D48: QApplicationPrivate::init() (in /Qt/6.5.3/gcc_64/lib/libQt6Widgets.so.6.5.3)
      ==161281==    by 0x109353: main (main.cpp:6)
      ==161281==
      ==161281== LEAK SUMMARY:
      ==161281==    definitely lost: 24 bytes in 1 blocks
      ==161281==    indirectly lost: 432 bytes in 5 blocks
      ==161281==      possibly lost: 352 bytes in 1 blocks
      ==161281==    still reachable: 95,625 bytes in 954 blocks
      ==161281==         suppressed: 0 bytes in 0 blocks
      ==161281== Reachable blocks (those to which a pointer was found) are not shown.
      ==161281== To see them, rerun with: --leak-check=full --show-leak-kinds=all
      ==161281==
      ==161281== For lists of detected and suppressed errors, rerun with: -s
      ==161281== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
      

      If I create more complicated programs with Qt, I get even more memory leaks.

      Please test your Qt functions in Valgrind to eliminate these kinds of memory leaks.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            liaqi Liang Qi
            donaldduck Donald Duck
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes