Results 1 to 9 of 9

Thread: Removing objects from memory - necessary?

  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 Removing objects from memory - necessary?

    Hello!

    I've got function in which I've got something like that:

    Qt Code:
    1. void MainWindow::myFunction()
    2. {
    3. [...]
    4.  
    5. bool ok;
    6. QTextStream out(stdout);
    7.  
    8. QSqlDatabase bdb = QSqlDatabase::addDatabase("QSQLITE");
    9.  
    10. bdb.setDatabaseName(databaseName);
    11. ok = bdb.open();
    12.  
    13. if (ok)
    14. {
    15. out << endl << "Otworzylem baze!" << endl ;
    16. } else {
    17. out << "Nie udalo sie otworzyc bazy!" << endl;
    18. }
    19.  
    20. QSqlQueryModel *queryModel = new QSqlQueryModel;
    21.  
    22. queryModel->setQuery("SELECT * FROM config WHERE name='qt-notification-interval'", bdb);
    23.  
    24. rec = queryModel->record(0);
    25. notificationInterval = (rec.value(1).toInt())*1000;
    26.  
    27. out << notificationInterval << endl;
    28.  
    29. bdb.close();
    30.  
    31. [...]
    32. }
    To copy to clipboard, switch view to plain text mode 

    I invoke this function many times. Is it necessary to remove somehow from memory queryModel? Or is it removed automatically after exiting function?

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Removing objects from memory - necessary?

    Create the query on the stack then you don't have to take care about. (and use a simple QSqlQuery if you don't have strong reasons using the model.)
    Qt Code:
    1. QSqlQueryModel queryModel;
    To copy to clipboard, switch view to plain text mode 
    So it gets deleted after the scope ends.

    EDIT: if you don't delete it yourself you create memory leaks and your applications memory need becomes big with the time.

  3. #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: Removing objects from memory - necessary?

    Quote Originally Posted by Lykurg View Post
    EDIT: if you don't delete it yourself you create memory leaks and your applications memory need becomes big with the time.
    I've forgot about memory leaks, and now my application is growing really fast, and sometimes variables changes their values (is it possible?!). I think thats the reason. I'll do as You said - without pointer.

    But if i really need to create object with pointer - how delete it?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 26th August 2010 at 14:18.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Removing objects from memory - necessary?

    Quote Originally Posted by Tomasz View Post
    But if i really need to create object with pointer - how delete it?
    When do not need the pointer anymore you can do it the old C++ way using:
    Qt Code:
    1. QWidget *w = new QWidget;
    2. delete w;
    To copy to clipboard, switch view to plain text mode 
    If it is QObject, or any class that has a QObject as base you can alos do:
    Qt Code:
    1. QWidget *w = new QWidget;
    2. w.deleteLater();
    To copy to clipboard, switch view to plain text mode 
    See QObject::deleteLater().

  5. #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: Removing objects from memory - necessary?

    I've tried delete on some widgets:

    Qt Code:
    1.  
    2. if(rec.value(1).toString()=="warning") msgBox->setIcon(QMessageBox::Warning);
    3. else if (rec.value(1).toString()=="info") msgBox->setIcon(QMessageBox::Information);
    4. else if (rec.value(1).toString()=="error") msgBox->setIcon(QMessageBox::Critical);
    5.  
    6. msgBox->setFont(font);
    7.  
    8. msgBox->setWindowTitle(rec.value(1).toString());
    9. msgBox->setText(rec.value(2).toString());
    10.  
    11. QSpacerItem* horizontalSpacer = new QSpacerItem(240, 290, QSizePolicy::Minimum, QSizePolicy::Minimum);
    12. QGridLayout* layout = (QGridLayout*)msgBox->layout();
    13. layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
    14.  
    15. msgBox->move(0,0);
    16. msgBox->exec();
    17.  
    18. delete msgBox;
    19. //delete horizontalSpacer;
    20. //delete layout;
    To copy to clipboard, switch view to plain text mode 

    but if I uncomment last two lines my program exits. Why?

    thanks in advance
    best regards
    Tomasz

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Removing objects from memory - necessary?

    your spacer and layout are children of msgBox. If you delete msgBox, all children gets deleted as well. That's a nice feature or Qt and its QObjects. Your program crashes because the spacer and layout does not exists at that time.

  7. The following user says thank you to Lykurg for this useful post:

    Tomasz (26th August 2010)

  8. #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: Removing objects from memory - necessary?

    So if I got something like that (program triggered by a button):

    Qt Code:
    1. void MainWindow::runQtApp()
    2. {
    3. QProcess *proc;
    4. proc = new QProcess( this );
    5. proc->startDetached("/home/myQtApp");
    6. }
    To copy to clipboard, switch view to plain text mode 

    The memory will be 'clean' after closing that 'myQtApp'?

    thanks in advance
    best regards
    Tomasz

  9. #8
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Removing objects from memory - necessary?

    No.
    Starts the program program with the arguments arguments in a new process, and detaches from it. Returns true on success; otherwise returns false. If the calling process exits, the detached process will continue to live.
    ...
    Windows: Arguments that contain spaces are wrapped in quotes. The started process will run as a regular standalone process.
    It will be delated when parent is deleted, so In Your case MainWindow.

    Use:
    Qt Code:
    1. void MainWindow::runQtApp()
    2. {
    3. QProcess *proc;
    4. proc = new QProcess( this );
    5. proc->startDetached("/home/myQtApp");
    6. delete proc;
    7. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Removing objects from memory - necessary?

    See also the documentation for QPointer.
    Last edited by Urthas; 29th August 2010 at 18:49.

Similar Threads

  1. Crash on removing a row
    By inktomi in forum Qt Programming
    Replies: 10
    Last Post: 17th June 2010, 12:59
  2. Replies: 3
    Last Post: 9th January 2010, 15:47
  3. Removing a directory entirely
    By Barry79 in forum Qt Programming
    Replies: 0
    Last Post: 11th May 2009, 15:09
  4. creating/deleting Objects // memory leaks
    By janus in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 18:17
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

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.