Results 1 to 19 of 19

Thread: memory leak

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default memory leak

    Hello, i'trying to understund why running my app with
    Qt Code:
    1. valgrind --tool=memcheck --leak-check=yes -v /my/app
    To copy to clipboard, switch view to plain text mode 
    i get a sort of memory leak
    Qt Code:
    1. 132 (4 direct, 128 indirect) bytes in 1 blocks are definitely lost in loss record 20 of 163
    To copy to clipboard, switch view to plain text mode 
    up here it just a little piece ....
    I'm using the "direct approach", what should i do to prevent it?
    It looks like when i call a function into my class in this way
    Qt Code:
    1. parent_object->myFunc()
    To copy to clipboard, switch view to plain text mode 
    i earn a leak....

    ----------------- edit -----------------
    to be more accurate my leak summury:
    Qt Code:
    1. ==3325== LEAK SUMMARY:
    2. ==3325== definitely lost: 708 bytes in 77 blocks.
    3. ==3325== indirectly lost: 22729 bytes in 420 blocks.
    4. ==3325== possibly lost: 0 bytes in 0 blocks.
    5. ==3325== still reachable: 728510 bytes in 26553 blocks.
    6. ==3325== suppressed: 0 bytes in 0 blocks.
    7. ==3325== Reachable blocks (those to which a pointer was found) are not shown.
    8. ==3325== To see them, rerun with: --show-reachable=yes
    9. --3325-- TT/TC: 0 tc sectors discarded.
    10. --3325-- 180208 tt_fast misses.
    11. --3325-- translate: new 89667 (1628736 -> 24489134; ratio 150:10)
    12. --3325-- discard 190 (2620 -> 35740; ratio 136:10).
    13. --3325-- chainings: 71514 chainings, 0 unchainings.
    14. --3325-- dispatch: 98933412 jumps (bb entries); of them 16549827 (16%) unchained.
    15. --3325-- 1979/607625 major/minor sched events.
    16. --3325-- reg-alloc: 15989 t-req-spill, 4351425+95101 orig+spill uis,
    17. --3325-- 484044 total-reg-rank
    18. --3325-- sanity: 1980 cheap, 80 expensive checks.
    19. --3325-- ccalls: 547959 C calls, 54% saves+restores avoided (1744510 bytes)
    20. --3325-- 726410 args, avg 0.90 setup instrs each (138758 bytes)
    21. --3325-- 0% clear the stack (1643130 bytes)
    22. --3325-- 156831 retvals, 34% of reg-reg movs avoided (103722 bytes)
    To copy to clipboard, switch view to plain text mode 
    Last edited by mattia; 8th January 2008 at 15:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Quote Originally Posted by mattia View Post
    It looks like when i call a function into my class in this way
    Qt Code:
    1. parent_object->myFunc()
    To copy to clipboard, switch view to plain text mode 
    i earn a leak....
    Then most likely myFunc() leaks. Do you create any objects using new operator inside it?

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    yes i do, for example
    Qt Code:
    1. void main()
    2. {
    3. ...
    4. funz();
    5. funz1();
    6. ...
    7. }
    8. void funz()
    9. {
    10. ...
    11. for ( int i = 0 ; i < list.size() ; i++ )
    12. {
    13. myTestObject * testObject;
    14.  
    15. testObject = new ( myTestObject , this );
    16.  
    17. temptwmConfigPool->doSomeThing( );
    18.  
    19.  
    20. myObjectList.append ( testObject );
    21.  
    22. delete testObject;
    23. }
    24. ...
    25. }
    To copy to clipboard, switch view to plain text mode 
    if i skip the delete statement it's ok otherwise i get a segmentation fault after it when i call the next function "funz1()", but funz1() does not use the object deleted...
    thx

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Do you use myObjectList in other functions?
    If so, don't delete testObject in funz(), instead iterate over myObjectList when finished with it and delete each object.

    This could explain your mem leaks and the crash when trying to delete testObject.

    Regards

  5. The following user says thank you to marcel for this useful post:

    mattia (9th January 2008)

  6. #5
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    the crash is triggered cos i try to use an object deleted...i more checked and it was a my mystake.
    But what should i do if i want to avoid this:
    Qt Code:
    1. void main::setUp( QTreeWidget * thetwmTreeWidget )
    2. {
    3. QList<QTreeWidgetItem *> items;
    4.  
    5. for ( int i = 0; i < list.size() ; i++ )
    6. {
    7. QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();
    8.  
    9. tempQTreeWidgetItem->setText ( 0 , list.at(1) );
    10.  
    11. items.append ( tempQTreeWidgetItem );
    12. }
    13. thetwmTreeWidget->setColumnCount (1);
    14. thetwmTreeWidget->insertTopLevelItems (0, items);
    15. }
    To copy to clipboard, switch view to plain text mode 
    When i create

    QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();

    don't i earn a memory leak?
    But i can't delete tempQTreeWidgetItem at the end of my application cos i have a scope problem, i can't see it, tempQTreeWidgetItem is just declared in setUp().
    I can't delete it at the setUp() end cos thetwmTreeWidget is used in my GUI.
    Is it right?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    If you want to delete an object you no longer use, you have to do that before it goes out of scope, because you won't be able to reach it later.

    In this case you don't have to delete the object that tempQTreeWidgeItem points to, because you add it to thetwmTreeWidget, which is responsible for deleting it.

  8. The following user says thank you to jacek for this useful post:

    mattia (9th January 2008)

  9. #7
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    Quote Originally Posted by jacek View Post
    If you want to delete an object you no longer use, you have to do that before it goes out of scope, because you won't be able to reach it later.

    In this case you don't have to delete the object that tempQTreeWidgeItem points to, because you add it to thetwmTreeWidget, which is responsible for deleting it.
    Thanks so much for the explanetion.
    Get back at my memory leak cos i'm not able to understand where i'm getting wrong.
    This is a little piece of valgrind command report:
    Qt Code:
    1. ==16797== 2848 (16 direct, 2832 indirect) bytes in 4 blocks are definitely lost in loss record 75 of 144
    2. ==16797== at 0x1B90939A: operator new(unsigned) (vg_replace_malloc.c:132)
    3. ==16797== by 0x818DE38: twmConfigObject::getPoolIconTooltip(int, QString) (twmConfig.cpp:4968)
    4. ==16797== by 0x81ADCB9: twmConfigObject::setupPoolsGetTreeView(QTreeWidget*, bool) (twmConfig.cpp:1210)
    5. ==16797== by 0x80BA4D0: twmMainWindow::setupPools(bool) (twmMainWindow.cpp:1387)
    6. ==16797== by 0x812DE02: twmMainWindow::twmMainWindow(QWidget*) (twmMainWindow.cpp:54)
    7. ==16797== by 0x80A0317: main (trm.cpp:62)
    To copy to clipboard, switch view to plain text mode 
    I think that the problem is in my twmConfigObject::getPoolIconTooltip(int, QString) method, and down here the method's code:
    Qt Code:
    1. QString twmConfigObject::getPoolIconTooltip ( int theStatus , QString poolName )
    2. {
    3. QString difference;
    4. countNotSync( TWMACTUALMODE__POOLS , false , & difference );
    5.  
    6. switch ( theStatus )
    7. {
    8. case POOLSTATUS__UNDEF:
    9.  
    10. if ( ! difference.isEmpty() && difference != 0)
    11. {
    12. return ( QString ( tr ( "Pool %1 status is: Undefined - %2 difference" ).arg ( poolName ).arg ( difference ) ) );
    13. }
    14. else
    15. {
    16. return ( QString ( tr ( "Pool %1 status is: Undefined" ).arg ( poolName ) ) );
    17. }
    18. break;
    19.  
    20. case POOLSTATUS__INCOMPLETESHARE:
    21.  
    22. if ( ! difference.isEmpty() && difference != 0)
    23. {
    24. return ( QString ( tr ( "Pool %1 status is: Incomplete share - %2 difference" ).arg ( poolName ).arg ( difference ) ) );
    25. }
    26. else
    27. {
    28. return ( QString ( tr ( "Pool %1 status is: Incomplete share" ).arg (poolName) ) );
    29. }
    30. break;
    31.  
    32. case POOLSTATUS__SHARED:
    33.  
    34. if ( ! difference.isEmpty() && difference != 0)
    35. {
    36. return ( QString ( tr ( "Pool %1 status is: Shared - %2 difference" ).arg ( poolName ).arg ( difference ) ) );
    37. }
    38. else
    39. {
    40. return ( QString ( tr ( "Pool %1 status is: Shared" ).arg (poolName) ) );
    41. }
    42. break;
    43.  
    44. default:
    45.  
    46. return ( QString ( tr ( "Terminal %1 status is: Undefined" ).arg (poolName) ) );
    47. break;
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 
    any hint?

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Is new operator used in countNotSync()?

  11. #9
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    No, i don't use any new operator in that function, but i call some other functions and there are not new statement into them.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Does the leak disappear, if you comment out all of the code and add "return QString();" instead?

  13. #11
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    i have a trouble to read the leak summary...
    Qt Code:
    1. #
    2. ==3325== definitely lost: 708 bytes in 77 blocks.
    3. #
    4. ==3325== indirectly lost: 22729 bytes in 420 blocks.
    5. #
    6. ==3325== possibly lost: 0 bytes in 0 blocks.
    7. #
    8. ==3325== still reachable: 728510 bytes in 26553 blocks. <-- have i lost them(728510bytes)?
    To copy to clipboard, switch view to plain text mode 

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Quote Originally Posted by mattia View Post
    have i lost them(728510bytes)?
    No, there's still some way to reach that data. For example Qt might store pointers somewhere inside itself.

    Whether that's a leak or not depends on whether this amount rises with time or not. For example if you have a loop where you create temporary QObjects, give them a parent and forget to delete them. You will get a leak, but the data is "reachable" all the time through the parent. On other hand you might create a single array using new operator and as long as there's only one such array you don't have a leak, even if you never free the memory it occupies.

  15. #13
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    ok, thx so much but is it a my business to delete it?
    from that you said up here it is not a my business to delete it, does Qt do it?

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memory leak

    Quote Originally Posted by mattia View Post
    ok, thx so much but is it a my business to delete it?
    Only if you are the owner of that memory. Although there is nothing wrong in having unallocated, but reachable memory at program exit.

  17. #15
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    I think i get the big problem, try to check please:

    Qt Code:
    1. ==26261== 4136 (2204 direct, 1932 indirect) bytes in 1 blocks are definitely lost in loss record 124 of 139
    2. ==26261== at 0x1B90939A: operator new(unsigned) (vg_replace_malloc.c:132)
    3. ==26261== by 0x809FC82: main (trm.cpp:62)
    To copy to clipboard, switch view to plain text mode 

    trm.cpp
    Qt Code:
    1. int main ( int argc, char *argv[] )
    2. {
    3. QApplication app ( argc, argv );
    4. ...
    5. ...
    6. // creating splash object
    7. QPixmap pixmap ( ":/trm/images/SplashScreen.png" );
    8. QSplashScreen * splash = new QSplashScreen ( pixmap );
    9. splash->show();
    10. splash->showMessage ( QObject::tr ("Loading settings..." ) , Qt::AlignRight | Qt::AlignBottom , Qt::black );
    11.  
    12. // creating main window
    13. twmMainWindow *mytwmMainWindow = new twmMainWindow(); // <-- trm.cpp:62
    14. mytwmMainWindow->show();
    15.  
    16. // closing and destroing splash screen
    17. splash->finish ( mytwmMainWindow );
    18. delete splash;
    19.  
    20. // run the main loop!
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Where hould i delete the statement in trm.cpp:62?
    I have to put it in my trm.h and in trm.cpp i have just to do
    mytwmMainWindow = new twmMainWindow();
    and destroy it at the end?
    Thx

  18. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: memory leak

    Just allocate it on the stack:
    Qt Code:
    1. twmMainWindow mytwmMainWindow(); // <-- trm.cpp:62
    2. mytwmMainWindow.show();
    To copy to clipboard, switch view to plain text mode 
    or alternatively set Qt::WA_CloseOnDelete attribute:
    Qt Code:
    1. twmMainWindow *mytwmMainWindow = new twmMainWindow(); // <-- trm.cpp:62
    2. mytwmMainWindow->setAttribute(Qt::WA_DeleteOnClose);
    3. mytwmMainWindow->show();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  19. #17
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    Quote Originally Posted by jpn View Post
    Just allocate it on the stack:
    Qt Code:
    1. twmMainWindow mytwmMainWindow(); // <-- trm.cpp:62
    2. mytwmMainWindow.show();
    To copy to clipboard, switch view to plain text mode 
    In this way i got this error
    Qt Code:
    1. error: request for member ‘show’ in ‘mytwmMainWindow’, which is of non-class type ‘twmMainWindow (
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by jpn View Post
    Just allocate it on the stack:
    or alternatively set Qt::WA_CloseOnDelete attribute:
    Qt Code:
    1. twmMainWindow *mytwmMainWindow = new twmMainWindow(); // <-- trm.cpp:62
    2. mytwmMainWindow->setAttribute(Qt::WA_DeleteOnClose);
    3. mytwmMainWindow->show();
    To copy to clipboard, switch view to plain text mode 
    In this way i'm able to run my app but nothing change...
    thx

  20. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: memory leak

    Quote Originally Posted by mattia View Post
    In this way i got this error
    Qt Code:
    1. error: request for member ‘show’ in ‘mytwmMainWindow’, which is of non-class type ‘twmMainWindow (
    To copy to clipboard, switch view to plain text mode 
    I didn't notice that class and variable names were exactly the same. So rename the variable name to be something different than what the class name is. For example:
    Qt Code:
    1. twmMainWindow mainWindow; // <-- trm.cpp:62
    2. mainWindow.show();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  21. The following user says thank you to jpn for this useful post:

    mattia (16th January 2008)

  22. #19
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: memory leak

    thanks so much! I learned a lot of cool stuff about memory leak and i fixed my problem.

Similar Threads

  1. Memory leak detection
    By Sid in forum Qt Programming
    Replies: 8
    Last Post: 4th May 2011, 22:38
  2. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  3. QPixMap and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 14:18
  4. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  5. Qt 4.1 Memory Leak
    By blackliteon in forum Qt Programming
    Replies: 14
    Last Post: 10th February 2006, 12:47

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.