Results 1 to 20 of 37

Thread: memory leak - where?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 13:28
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08: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.