Results 1 to 6 of 6

Thread: Qt example with memory leak

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt example with memory leak

    Hello.
    I compiled a basic GUI example written with Qt. When I run with valgrind, it tells that there is memory leak.
    It is stranger, because at each time I run the app, the number of malloc calls are different.
    Qt seems nondeterministic and it does not free allocated memory.

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QPushButton hello("Hello world!");
    9. hello.resize(100, 30);
    10.  
    11. hello.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt example with memory leak

    Qt does its own memory management for several classes that escapes detection by valgrind's checks for matched new/delete and malloc/free calls.

    If you like, you can build a debug version of Qt and track these down yourself. When we did that, we wound up convincing ourselves that A) all the "leaks" we checked were spurious, and B) had we discovered any actual leaks, it would have been largely out of our control to fix them, since memory allocation takes place in code that could change with any future release.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt example with memory leak

    On my Linux machine with Qt 4.6.3 I see lost memory reports (possible and concrete) in libglib, libgobject, libfreetype, libX11 and few others. None of the leaks come directly from Qt.

  4. #4
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt example with memory leak

    Is there a way to detect actual memory leaks in a code that uses Qt?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt example with memory leak

    You already have it. Valgrind is a powerful tool but it is neither a no-brainer tool nor totally infallible. This might shed some light on the types of "leaks":
    http://www.linuxprogrammingblog.com/...g-memory-leaks

  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt example with memory leak

    IMHO valgrind is useless to debug a qt program without a suppression file. Buts very easy to create one. Write a very simple Qt Programm (only a few lines, but with QApplication, Dialog, etc; like the one in your example) and then use the Valgrind Suppression File Howto to create one. You will get far less "qt errors" and can focus on your own code. Really nice is the Valgrind Eclipse Integration. That makes valgrind usage easier (at least for me )

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  3. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  4. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08:02
  5. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.