Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: Basic question on new and delete

  1. #21
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Quote Originally Posted by GreyGeek
    That I knew, I was just supposing that if they were created by "new" would it matter if they were deleted using the delete operator because they were going out of scope anyway if, as you say, the OS will always get the memory back.
    Ok, then no, it wouldn't matter. As a matter of good practice, I would delete them anyway though.

    Because it inherited from QDialog?
    class dlgLogin : public QDialog

    An ancestor isn't a parent?
    (I told you I was new to C++)
    No, QDialog is its baseclass, not its parent. The parent is the object you pass as the QWidget* parameter (which can be NULL). 'Baseclass', 'superclass' or 'inherited class' are names for the same C++ OO constructs. 'Parent' (in this context) is a Qt construct and refers to ownership, not inheritance. Reading the comprehensive documentation of QObject will explain it more.

    So, using "new" as shown below means that "ui.gridMultiProp" is a parent to *model ??
    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(ui.gridMultiProp);
    To copy to clipboard, switch view to plain text mode 
    That's correct.

    Pardon the dumb questions but you'll dealing with someone who has read perhaps too much and it's all jumbled up inside?
    It's amazing that I can, using QT, write the app I did and that it works.
    It is amazing. Did you check your prog for memory leaks
    Actually, it's a very bad idea to use Qt to learn C++ (not suggesting that you did it this way). It is much better to learn C++ and get confident with it (warts and all), then start using Qt to see how much easier it makes your life! The QObject parent-child model along with the references counted non-QObjects really help cut down on the amount of memory management that you have to worry about (but you still must be wary).

  2. #22
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Once the program has terminated. The OS (Linux and Windows) will be able to use the memory again. Definition of memory leak is during the lifetime of the program.

    However, the OS might not be so nice for awhile if the memory leaking program took up ALL memory resources until it finally crashed. But the first example is a safe example. He makesnew object... (usually doing other stuff too ;-) and then the application quits, and the memory - when it is needed. Will be re-used by the OS.

    Another issue is memory fragmentation, which can be a bigger problem on Windows I fear.

  3. #23
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Welll I have a very basic query on this matter.. What happens if the application is run continuously where it will have several instances of new.
    Lets say at the end of the program it says:
    Do you want to quit or continue ?

    If the user wants to continue then the main application has not been quit.. so all the instances of "new" that were originally there will again be made..as the program will again run from the beginning .
    In such case does QT takes care of freeing the memory..or we have to manually free up all the memory taken by the object ..

  4. #24
    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: Basic question on new and delete

    Quote Originally Posted by ct
    In such case does QT takes care of freeing the memory..or we have to manually free up all the memory taken by the object ..
    Qt will delete only objects derived from QObject that have a parent and only if their parent gets deleted.

  5. #25
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    so say in case of something like messenger, where one logs out all the objects needs to be manuallly freed so that new instances could be formed for the new sign in procedure ..

  6. #26
    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: Basic question on new and delete

    Quote Originally Posted by ct
    so say in case of something like messenger, where one logs out all the objects needs to be manuallly freed so that new instances could be formed for the new sign in procedure ..
    If they aren't QObjects or they have no parents, yes.

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.