Results 1 to 13 of 13

Thread: Memory leaks with Valgrind on a simple code with QUiLoader

  1. #1
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Memory leaks with Valgrind on a simple code with QUiLoader

    Hi,

    Maybe too much hours working, maybe I have been looking the code for too much time, maybe I don't understand exactly the documentation, or maybe there are no error, but I have some memory leaks with this code and I feel unable to find a solution. I'm trying to load from a ui file a QMainWindow.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTranslator>
    3. #include <QObject>
    4. #include <QUiLoader>
    5. #include <QFile>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QMainDlg *mainWin = 0;
    10.  
    11. QApplication app(argc, argv);
    12.  
    13. // Cargamos el archivo con las traducciones
    14. QTranslator translator;
    15. if (translator.load("qt_es", app.applicationDirPath()) == false) {
    16. QMessageBox::warning(0, QString::fromUtf8(APP_NAME),
    17. QObject::trUtf8("Ha sido imposible cargar el fichero con las traducciones."));
    18. } else {
    19. app.installTranslator(&translator);
    20. }
    21.  
    22. QString fileName = QString("/home/david/tmp/main.qmaindlg.ui");
    23. QUiLoader uiLoader;
    24.  
    25. if ( QFile::exists(fileName) ) {
    26. QFile file (fileName);
    27. file.open( QFile::ReadOnly );
    28. QWidget *wid = uiLoader.load(&file, 0);
    29. if ( wid != NULL ) {
    30. wid->setAttribute(Qt::WA_DeleteOnClose);
    31. wid->show();
    32. }
    33. file.close();
    34. bool rc = app.exec();
    35. return rc;
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    I simplify the main.qmaindlg.ui file to this:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>QMainDlg</class>
    4. <widget class="QMainWindow" name="QMainDlg">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>790</width>
    10. <height>126</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>Presupuestos - Pinelo Talleres Gráficos</string>
    15. </property>
    16. <property name="windowIcon">
    17. <iconset>
    18. <normaloff>:/aplicacion/recursos/icono_app_peque.png</normaloff>:/aplicacion/recursos/icono_app_peque.png</iconset>
    19. </property>
    20. <widget class="QWidget" name="centralwidget"/>
    21. </widget>
    22. <connections/>
    23. </ui>
    To copy to clipboard, switch view to plain text mode 

    I use the Qt47supp.txt provides on Nokia Labs... The output of Valgrind is attached. You can see a lot of "definitely lost". valgrind.output.txt.zip

    Is this normal? Something wrong with my code? I have no idea and I have several crashes on my applications, related with this, I think.

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    I can't check at the moment to be 100% sure, but it makes sense to me that QUiLoader doe NOT delete widgets it created, since it would mean an instance of QUiLoader would have to be kept alive as long as the widgets it created are alive.
    And since you are not deleting the widgets you created with QUiLoader, you have a leak.

    Qt Code:
    1. QWidget *wid = uiLoader.load(&file, 0);
    To copy to clipboard, switch view to plain text mode 

    Try adding a clean up code for wid, or give it a parent, and see if the leaks are gone.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Thanks for your answer. I know that QUiLoader does not delete widgets. It only create this. You have to assign a parent to the widget, or use
    Qt Code:
    1. wid->setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 
    like I use on the code. I'm not sure if this attribute is enough to ensure that the widget will be deleted.
    Anyway, I use QUiLoader on other places on my app, and I assign a parent to the widget recently created, and I get the same leaks.

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Sorry, I was reading the code to fast, I didn't notice that line.
    Hmm- did you check the Qt bug tracker, maybe its has been reported as a bug?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Yes, I searched and found some bugs similar but not with QUiLoader. I asked here before open a new bug. But if you, or someone can test my code, and see no error on the code or in my "understanding" of QUiLoader, I will open the bug.

    I have a similar problem using QtScript... But this one in a new post.

    Thanks a lot again.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Change the line
    Qt Code:
    1. QWidget *wid = uiLoader.load(&file, 0);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QWidget *wid = new QWidget();
    To copy to clipboard, switch view to plain text mode 
    and see if leaks are still reported.

  7. #7
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Hi,

    If you see the documentation of QUiLoader, the load method says:

    "Loads a form from the given device and creates a new widget with the given parentWidget to hold its contents."

    So I understand that QUiLoader::load creates (make the "new") the widget.

    Anyway I will test your suggestion.

    Thanks a lot.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Quote Originally Posted by Sparhawk View Post
    Anyway, I use QUiLoader on other places on my app, and I assign a parent to the widget recently created, and I get the same leaks
    Please provide a minimal compilable example reproducing the problem. Make sure the "leaks" are indeed leaks and not false positives or leaks caused by 3rd party libs. As far as I know Qt doesn't leak memory. I went briefly through your valgrind report and most of the "definitely lost" reports are either false positives or possible leaks caused by external libs.
    Last edited by wysota; 8th April 2011 at 12:27.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    The code I put on the original post is compilable (unless I've been wrong with cut&paste). I'm not sure that the leaks are false positive: I mean, I use the suppress rules provide by Qt developers on Nokia blogs...
    I think it is not a Qt problem... but I can't explain the Valgrind output. Maybe is related with the library used to display QIcon? I don't know...

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Quote Originally Posted by Sparhawk View Post
    The code I put on the original post is compilable (unless I've been wrong with cut&paste)
    But it's not minimal. You're using a translator and some pixmaps. Remove those and then compare valgrind report with the original one. If you want to test QUiLoader then test QUiLoader and not a bunch of other stuff.

    I'm not sure that the leaks are false positive: I mean, I use the suppress rules provide by Qt developers on Nokia blogs...
    I'm sure many of them are false positives - like all the ones related to image plugins.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    If you see the documentation of QUiLoader, the load method says:
    Yes, I know, I just want you to verify that this leak report is related to QUiLoader::load(). I don't have possibility to test on linux now, also try if this code will cause "leak":
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char ** argv){
    4. QApplication app(argc,argv);
    5. QWidget * w = new QWidget();
    6. w->setAttribute(Qt::WA_DeleteOnClose);
    7. w->show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    I just think that QUiLoader has nothing to do with the "leak" report, its more the Qt::WA_DeleteOnClose flag. Of course I may be wrong, but I've been using QUiLoader without problems.
    Last edited by stampede; 8th April 2011 at 12:45. Reason: quoted text

  12. #12
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    4

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    I test the code that stampede writes with Valgrind. The ouput is attached valgrind.log.zip. There are some "definitely lost" entries. I think it can't be Qt... I'm not sure what to do now. Has anyone made the same test?

    Thanks

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

    Default Re: Memory leaks with Valgrind on a simple code with QUiLoader

    Most of the leaks are emanating from X, which is known to have memory leaks. There's nothing you can do about those. The others seem to be related to system calls; these might be actual leaks, or they might be an artifact of the kernel's internal memory management that don't actually leak memory. Like the X leaks, there's nothing you can do about them.

    If you run your program for an extended period of time and exercise all of it's functionality over and over again, and the amount of memory leaked stays relatively constant, I'd quit worrying about it. You'll pay a small, fixed cost per application startup until someone gets around to fixing them, if they ever do.

    If memory use grows continuously over time, it might be worth a more detailed look.

Similar Threads

  1. Memory Leaks
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2008, 16:26
  2. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 19:28
  3. Memory leak Detection using Valgrind
    By joseph in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 07:34
  4. Valgrind and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 12
    Last Post: 13th August 2007, 10:25
  5. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

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.