Page 1 of 2 12 LastLast
Results 1 to 20 of 37

Thread: memory leak - where?

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default memory leak - where?

    Hello!

    I've got my simple application, but it causes memory leak. I think one of this three functions is a problem:

    Qt Code:
    1. bool MainWindow::pobierzKomunikaty()
    2. {
    3. bool ok;
    4. QTextStream out(stdout);
    5. QString zapytanie;
    6. QSqlQueryModel queryModel;
    7.  
    8. QFont font;
    9. font.setPointSize(7);
    10.  
    11. odczytanoOstatnieKomunikaty=0;
    12.  
    13. ok = bdb.open();
    14.  
    15. if (ok)
    16. {
    17. out << endl << "Otworzylem baze!" << endl ;
    18. } else {
    19. out << "Nie udalo sie otworzyc bazy!" << endl;
    20. }
    21.  
    22. queryModel.setQuery("SELECT * FROM server_notifications WHERE complete='0'", bdb); //LIMIT 1
    23.  
    24. liczbaWiadomosci = queryModel.rowCount();
    25.  
    26. bdb.close();
    27.  
    28. out << "Mamy: " << liczbaWiadomosci << " wiadomosci!" << endl;
    29.  
    30.  
    31. for(int i=0 ; i<liczbaWiadomosci; i++)
    32. {
    33. rec = queryModel.record(i);
    34.  
    35.  
    36. out << rec.value(1).toString() << ": ";
    37. out << rec.value(2).toString() << endl;
    38.  
    39. QMessageBox *msgBox=new QMessageBox;
    40.  
    41. if(rec.value(1).toString()=="warning") msgBox->setIcon(QMessageBox::Warning);
    42. else if (rec.value(1).toString()=="info") msgBox->setIcon(QMessageBox::Information);
    43. else if (rec.value(1).toString()=="error") msgBox->setIcon(QMessageBox::Critical);
    44.  
    45. msgBox->setFont(font);
    46.  
    47. msgBox->setWindowTitle(rec.value(1).toString());
    48. msgBox->setText(rec.value(2).toString());
    49.  
    50. QSpacerItem* horizontalSpacer = new QSpacerItem(240, 290, QSizePolicy::Minimum, QSizePolicy::Minimum);
    51. QGridLayout* layout = (QGridLayout*)msgBox->layout();
    52. layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
    53.  
    54. msgBox->move(0,0);
    55. msgBox->exec();
    56.  
    57. delete msgBox;
    58.  
    59. ok = bdb.open();
    60.  
    61. zapytanie.clear();
    62. zapytanie="update server_notifications set complete='1'";
    63. zapytanie.append(" where id='");
    64. zapytanie.append(QString::number(rec.value(0).toInt()));
    65. zapytanie.append("'");
    66.  
    67. QSqlQuery *query=new QSqlQuery;
    68.  
    69. query->clear();
    70. query->prepare(zapytanie);
    71. ok = query->exec();
    72.  
    73. bdb.close();
    74.  
    75. delete query;
    76. }
    77.  
    78. odczytanoOstatnieKomunikaty=1;
    79.  
    80. return ok;
    81. }
    82.  
    83. bool MainWindow::wyswietlKomunikaty()
    84. {
    85. if(odczytanoOstatnieKomunikaty==0)
    86. {
    87.  
    88. } else
    89. {
    90. pobierzKomunikaty();
    91. odczytajNotificationInterval();
    92. timer->start(notificationInterval);
    93. }
    94.  
    95. return 1;
    96. }
    97.  
    98. bool MainWindow::odczytajNotificationInterval()
    99. {
    100. bool ok;
    101. QTextStream out(stdout);
    102. QSqlQueryModel queryModel;
    103.  
    104. ok = bdb.open();
    105.  
    106. if (ok)
    107. {
    108. out << endl << "Otworzylem baze!" << endl ;
    109. } else {
    110. out << "Nie udalo sie otworzyc bazy!" << endl;
    111. }
    112.  
    113. queryModel.setQuery("SELECT * FROM config WHERE name='qt-notification-interval'", bdb);
    114.  
    115. rec = queryModel.record(0);
    116. notificationInterval = (rec.value(1).toInt())*1000;
    117.  
    118. out << "Notification interval: " << notificationInterval << " ms" << endl;
    119.  
    120. bdb.close();
    121.  
    122. return ok;
    123. }
    To copy to clipboard, switch view to plain text mode 

    Maybe someone can tell me where?

    thanks in advance
    best regards
    Tomasz

  2. #2
    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 leak - where?

    There is no memory leak here. But you are needlessly creating objects on heap and not on the stack (like the QSqlQuery object).
    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.


  3. The following 2 users say thank you to wysota for this useful post:

    anarion (2nd September 2010), Tomasz (1st September 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    So I should create all the objects one time in my *.h file as a private objects?
    One question to my code - after exiting each function all the objects are being deleted from memory?

    And one more piece of code from beginning:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    7.  
    8. databaseName="/mybase";
    9. bdb = QSqlDatabase::addDatabase("QSQLITE");
    10. bdb.setDatabaseName(databaseName);
    11.  
    12. connect(ui->pomiaryButton, SIGNAL(clicked()), this, SLOT(uruchomPomiary()));
    13. connect(ui->sterowanieButton, SIGNAL(clicked()), this, SLOT(uruchomSterowanie()));
    14. connect(ui->klimaButton, SIGNAL(clicked()), this, SLOT(uruchomKlima()));
    15. connect(ui->konfiguracjaButton, SIGNAL(clicked()), this, SLOT(uruchomKonfiguracja()));
    16.  
    17. odczytanoOstatnieKomunikaty=0;
    18.  
    19. odczytajNotificationInterval();
    20.  
    21. timer = new QTimer(this);
    22. connect(timer, SIGNAL(timeout()), this, SLOT(wyswietlKomunikaty()));
    23. timer->start(notificationInterval);
    24.  
    25. this->show();
    26.  
    27. pobierzKomunikaty();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Now it's the whole code. Maybe somewhere here is memory leak? I'm thinking about memory leak because each time I check how many resources programs needs (by 'ps' in linux console) my application take more resources.

    thanks in advance
    best regards
    Tomasz

  5. #4
    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 leak - where?

    Quote Originally Posted by Tomasz View Post
    So I should create all the objects one time in my *.h file as a private objects?
    No. If you have an "int" variable somewhere in your code do you create it as:
    Qt Code:
    1. int x;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. int *x = new int;
    To copy to clipboard, switch view to plain text mode 
    ?

    Are any of those "private obejcts"?

    One question to my code - after exiting each function all the objects are being deleted from memory?
    The ones that are created on the stack are deleted automatically as the stack unwinds, the ones created in dynamic memory (heap) have to be released manually (unless they are parented QObjects, I'm sure you know the drill...).

    Maybe somewhere here is memory leak?
    If you don't have a corresponding "removeDatabase" to each "addDatabase" I suspect there could be a memory leak. I'm not sure if Qt releases the old connection when it detects a new one with the same name (but you surely get warning messages to the console if you are trying to reuse name of an active db connection).
    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.


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

    Tomasz (1st September 2010)

  7. #5
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    I create my "private" variables as:

    Qt Code:
    1. int x;
    To copy to clipboard, switch view to plain text mode 

    But what about this objects in functions - all of them are created on heap or stack? I need to remove them?

    thanks in advance
    best regards
    Tomasz

  8. #6
    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 leak - where?

    Quote Originally Posted by Tomasz View Post
    I create my "private" variables as:

    Qt Code:
    1. int x;
    To copy to clipboard, switch view to plain text mode 
    What about others? Have you ever (anywhere) created an int using the "new" operator?
    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. #7
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    The only thing created with "new" is timer:

    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. QString databaseName;
    4. int liczbaWiadomosci;
    5. QTimer *timer;
    6. int odczytanoOstatnieKomunikaty;
    7. int notificationInterval;
    To copy to clipboard, switch view to plain text mode 

    So I don't need to remove variables that I have in my function manually? Because I never did that in "pure" C/C++. As I remember I only need to remove things created with "new" operator.

    thanks in advance
    best regards
    Tomasz

  10. #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 leak - where?

    Quote Originally Posted by Tomasz View Post
    The only thing created with "new" is timer:
    Look at the first snippet you provided and examine that. Especially the QSqlQuery instance.
    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. #9
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    Yes, You're right, but after using it I'm deleting it, right?

    thanks in advance
    best regards
    Tomasz

  12. #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 leak - where?

    Quote Originally Posted by Tomasz View Post
    Yes, You're right, but after using it I'm deleting it, right?
    Yes, but there is no point in creating it on the heap (using the "new" operator) and having to worry about deleting it afterwards. Simply create the query and the message box on the stack and the compiler will take care of deleting them for you.
    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.


  13. The following user says thank you to wysota for this useful post:

    anarion (2nd September 2010)

  14. #11
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    So, reassuming. Now If I don't change anything (later I will take care about what You said) there is no reason to worry about memory leak in this code above?

    thanks in advance
    best regards
    Tomasz

  15. #12
    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 leak - where?

    Except the database issue, yes. But in general it's best to run your code through memory leak detection tool such as valgrind and see what it has to say.
    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.


  16. The following user says thank you to wysota for this useful post:

    Tomasz (1st September 2010)

  17. #13
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    I'll check it with valgrind. Is it necessary to use removeDatabase if I'm adding it only once at the beginning and then only using open/close on that database? Should I remove it and add it again after every open/close cycle?

    thanks in advance
    best regards
    Tomasz

  18. #14
    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 leak - where?

    If you add it once then you remove it once. You said your application was increasing its memory usage and you gave the code snippet where you suspected the leak to be so I assumed it was being ran multiple times. We don't consider a leak something that lives throughout the whole life of the application and is not deleted anywhere (although it's still considered "improper"). A leak is when you repeat this process thoughout the life of the application, like so:
    Qt Code:
    1. for(int i=0;i<10;i++){
    2. QString *var = new QString("xxx"); // memory is allocated 10 times and never released although the variable is not reachable anymore
    3. }
    To copy to clipboard, switch view to plain text mode 
    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.


  19. #15
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    I've used valgrind, and it says:

    Qt Code:
    1. LEAK SUMMARY:
    2. definitely lost: 768 bytes in 4 blocks
    3. indirectly lost: 2,840 bytes in 141 blocks
    4. possibly lost: 1,119,616 bytes in 4,885 blocks
    5. still reachable: 585,094 bytes in 5,456 blocks
    6. suppressed: 0 bytes in 0 blocks
    To copy to clipboard, switch view to plain text mode 

    Maybe I can post full code of my app (it's not long) and someone could tell me what I'm doing wrong?

    thanks in advance
    best regards
    Tomasz

    P.S. Even if I run the simplest application valgrind says that some memory is lost, but not as much as above.

  20. #16
    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 leak - where?

    Rerun valgrind with --leak-check=full --show-reachable=yes
    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.


  21. #17
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    I've ran it with that options for a while and it says:

    Qt Code:
    1. LEAK SUMMARY:
    2. definitely lost: 1,792 bytes in 8 blocks
    3. indirectly lost: 8,400 bytes in 418 blocks
    4. possibly lost: 5,756,014 bytes in 7,557 blocks
    5. still reachable: 1,967,661 bytes in 8,582 blocks
    6. suppressed: 0 bytes in 0 blocks
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: memory leak - where?

    Quote Originally Posted by Tomasz View Post
    I've ran it with that options for a while and it says:

    Qt Code:
    1. LEAK SUMMARY:
    2. definitely lost: 1,792 bytes in 8 blocks
    3. indirectly lost: 8,400 bytes in 418 blocks
    4. possibly lost: 5,756,014 bytes in 7,557 blocks
    5. still reachable: 1,967,661 bytes in 8,582 blocks
    6. suppressed: 0 bytes in 0 blocks
    To copy to clipboard, switch view to plain text mode 
    Well, it says a good deal more than that. Valgrind gives you a detailed report not only on the total amount of memory leaked, but where it was leaked, right down to the line number. You'll want to compile in debug mode to get the best report, but even without debugging information it's usually possible to track down the portion of code responsible for a leak.

    Read through the valgrind documentation so you know what you're looking at.

  23. #19
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: memory leak - where?

    Quote Originally Posted by SixDegrees View Post
    Well, it says a good deal more than that.
    Yes, It says really a lot of things. Sometimes it says about errors even in 'clean' application (window only) just created in Qt Creator. I don't have a lot of experience with valgrind, I'm reading documentation but still have no idea how to track that leaks. Code looks fine for me.

    thaks in advance
    best regards
    Tomasz

  24. #20
    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 leak - where?

    Sometimes you can get false positives so it's important to understand why certain things are reported. But you should get a very detailed output with a place in code and reason for every leak.
    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.


Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 12:13
  2. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 14:28
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 11:22
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 17:24
  5. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 09: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.