Results 1 to 13 of 13

Thread: Auto-generate ui header & memory leak?

  1. #1
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Auto-generate ui header & memory leak?

    I'm checking where is the memory-leak happening in my app.And find out this:
    Qt Code:
    1. void setupUi(QWidget *MyWidgetClass)
    2. {
    3. ...
    4. hLayout1 = new QHBoxLayout();
    5. ...
    6. verticalLayout = new QVBoxLayout();
    7. ...
    8. horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    9. ...
    10. verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    11. ...
    12. } // setupUi
    To copy to clipboard, switch view to plain text mode 
    In one of the generated ui header,in the setupUi() function,there are things created in the heap.And they don't set the parent.So,when I'm done with the QWidget,this would defintely cause memory leak if I don't manually delete them.

    I know nothing is perfect,but I thought the generated file would deal with the parent thing.
    So each time it generate a new version of file,do I have to check if they have set the widget's parent?Is there a way avoid this?

    BTW,does someone how if there's a free memory-leak detector for c++(Windows platform)?



    ----------------------EDIT-----------------------------
    OK,I see that they are added to another layout.Then they would be deleted if the custom QWidget is deleted.So,where is the common place of the leak?Where should I pay attention to?
    Last edited by MorrisLiang; 20th May 2010 at 12:35.
    It's not the goodbye that hurts,but the flashback that follow.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    There is no "someObject->addLayout(hLayout1)" for example?

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Auto-generate ui header & memory leak?

    The auto generated setupUi(QWidget *MyWidgetClass) will be called most likely in the class constructor with parameter "this" witch will parent all the members.

    *** The widgets can be created without parent, if they are added to a layout, they get "adopted" even if the layout don't have a parent, he gets one when we call some_widget->setLayout(...), and set all widgets parent.

    Post the complete source code of setupUI, or look carefully, you will see that eventualy everything gets parents.

    LE: if some widget remains without a parent, you will notice that it won't appear when you call show() on parent (because you don't show each individual widget separate, you call show on one instance of the class )

    *** To express correct: the layout doesn't actually have a parent: QHBoxLayout *l = new QHBoxLayout(window); means that all the widgets added to the layout l will be auto-parented to window.
    Last edited by Zlatomir; 20th May 2010 at 13:06.

  4. #4
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    Yeah,I look at the code again, and find the line
    "someObject->addLayout(hLayout1);"
    Everything has a parent,so they're not causing the leak.

    And here's another problem:
    At first,my app is using 17000K memory.
    I have a button,clicking it will create a statck-based custom dialog.This action costs nearly 1M memory.So now it's 18000K.
    Then I close it,I get back 300K,that is 17700K now.
    But shouldn't it go back to 17000K when I close the dialog?
    It's not the goodbye that hurts,but the flashback that follow.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Auto-generate ui header & memory leak?

    Disclaimer: I'm not an expert in operating systems,

    But i'm pretty sure that what you see in task-manager (or Linux/other OS or utility) isn't the actual memory occupied by your application, but it is the memory that your OS "spare" for your application (so if you close the 1M dialog, the OS doesn't instantly re-claims the 1M memory, it leaves your app with a little more, so it wont have to allocate/de-allocate memory, if you open the dialog again, so your app will have a "stack" in total memory available for the OS)

  6. The following user says thank you to Zlatomir for this useful post:

    MorrisLiang (20th May 2010)

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

    Default Re: Auto-generate ui header & memory leak?

    Are you sure you are measuring the memory in the appropriate way? System tools will likely give you an incorrect result.
    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.


  8. #7
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    I'm watching windows task manager actually.
    Thanks for all the information,I try to open/close the dialog several times,the memory doesn't boost.So,I guess it's not a leak.
    It's not the goodbye that hurts,but the flashback that follow.

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

    Default Re: Auto-generate ui header & memory leak?

    Quote Originally Posted by MorrisLiang View Post
    I'm watching windows task manager actually.
    Yep, that's one of the tools giving incorrect values
    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.


  10. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    There are much better tools, such as Valgrind.

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

    Default Re: Auto-generate ui header & memory leak?

    Or any dedicated to tracking memory use implementation of malloc and free.
    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.


  12. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    I've changed malloc and free before - it doesn't catch new and delete calls

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

    Default Re: Auto-generate ui header & memory leak?

    Quote Originally Posted by fatjuicymole View Post
    I've changed malloc and free before - it doesn't catch new and delete calls
    I'm sure there are ways to do it. I think even Qt offers to substitute malloc with something else (but I may as well be day dreaming right now). Valgrind is unfortunately not an option for people who develop only on Windows.
    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.


  14. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-generate ui header & memory leak?

    For Windows people, Visual C++ provides built-in memory leak detection.

  15. The following user says thank you to squidge for this useful post:

    MorrisLiang (21st May 2010)

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  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 zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02

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
  •  
Qt is a trademark of The Qt Company.