Results 1 to 5 of 5

Thread: creating/deleting Objects // memory leaks

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default creating/deleting Objects // memory leaks

    hi,
    since a year or so I am learning to use c++ and qt. I have writen a small app and since Qt is quite easy to use and well documented it works all fine. But i guess their are still alot of things i can improve. I have a question regarding memory leaks and the cration of objects. I did not take very much care of that so far (maybe a silly question):
    As far as I understand childobjects are destroyed when the parent is destroyed. What does that mean when I use e.g. a QSqlTableModel or a QTableView etc.. Do I have to create them allways like ... = new QSqlTableModel(this) instead of ... = new QSqlTableModel ?
    Is the model not destroyed after the mainwindow is closed when i create it without assigning it to a parent (the QMainWindow) ?

  2. #2
    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: creating/deleting Objects // memory leaks

    Quote Originally Posted by janus View Post
    As far as I understand childobjects are destroyed when the parent is destroyed.
    That's correct. But that's all, there is no black magic like that all QObjects would get somehow cleaned up. They won't unless they have a parent AND their parent gets deleted. If the parent isn't deleted, children aren't either.

    What does that mean when I use e.g. a QSqlTableModel or a QTableView etc.. Do I have to create them allways like ... = new QSqlTableModel(this) instead of ... = new QSqlTableModel ?
    Is the model not destroyed after the mainwindow is closed when i create it without assigning it to a parent (the QMainWindow) ?
    No, the model isn't destroyed unless you pass a parent.
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: creating/deleting Objects // memory leaks

    thank you
    just a clarification for a beginner :-) :

    I should not use this code examples from the docs (QSqlQueryModel Class Reference) ...

    Qt Code:
    1. model->setQuery("SELECT name, salary FROM employee");
    2. model->setHeaderData(0, Qt::Horizontal, tr("Name"));
    3. model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    To copy to clipboard, switch view to plain text mode 

    without adding ... = new QSqlQueryModel(this); ?

    I guess everyone knows (besides me .-)
    Last edited by jpn; 27th March 2008 at 18:06. Reason: missing [code] tags

  4. #4
    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: creating/deleting Objects // memory leaks

    Well, of course those example snippets expect you to know the basic concepts of the library itself and also the used programming language. Like if you create a model on the stack like in the next example snippet, it might go out of scope unless there's for example QApplication::exec() which blocks. It's always the same problem with example code snippets. If there was "this" then people would surely ask what's "this" referring to. Usually it's pointless to make example snippets complete for the sake of clarity and readability.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: creating/deleting Objects // memory leaks

    yes, you are right. Anyway, now i am going to add many "this(es)" to my little app .-)
    thx

Similar Threads

  1. Memory leaks when read/write to DOM
    By SailinShoes in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2008, 11:51
  2. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00
  3. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

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.