Results 1 to 5 of 5

Thread: Dynamically allocated objects

  1. #1
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Question Dynamically allocated objects

    Hello!

    May be it will be a stupid question, but I want to know it for sure.
    Does the same principle work in QT as in C++: what was created dynamically, suppose to be deleted with a help of key-word "delete"?
    For instance, I have the next line of code: QListView *_lv = new QListView; Will be deleted automatically _lv-variable or in the end I suppose delete it by myself?
    And next example: I have my own class "myClass" which is derived from, let say, QGraphicsItem and the next line of code: myClass *_mc = new myClass();. The question is the same.

    Thank you beforehand for answers.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Dynamically allocated objects

    The answer is the same for both questions:

    If you have only QListView *lv = new QListView; and lv doesn't get a parent (for example by adding it into a layout) you should call delete.

    But if you have QListView *lv = new QListView(SomeWidgetPointer); then lv gets deleted when SomeWidgetPointer gets deleted - either by it's parent or by a delete call or gets out of scope (if the parent is allocated on stack)

    So basically you need to make sure the QWidgets (and other QObject derived classes) get a parent and then you need to make sure you delete the top-most parent (or allocate it on stack - for example in main function)

    //hope i was clear enough

  3. The following user says thank you to Zlatomir for this useful post:

    DmitryNik (25th September 2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically allocated objects

    Thank you. Nice to know such things for preventing memory leaking.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dynamically allocated objects

    You are responsible for delete-ing anything you allocate in C++, Qt does not change that. If the thing you allocate is a QObject with a parent (or give it to something that sets the parent) then the QObject parent-child hierarchy looks after it for you. For other objects you allocate you can look at Qt's QScopedPointer and QSharedPointer, which are roughly similar to the std::auto_ptr and std::tr1::shared_ptr, to help with this task.

  6. #5
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically allocated objects

    When your object has a parent, if you do not delete it, it will be deleted by parent

Similar Threads

  1. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  2. appBar: allocated space problem
    By mito in forum Qt Programming
    Replies: 4
    Last Post: 29th February 2008, 12:17
  3. dynamcly allocated buttons
    By MarkoSan in forum Qt Programming
    Replies: 28
    Last Post: 14th January 2008, 00:08
  4. Replies: 8
    Last Post: 20th April 2007, 00:37
  5. Replies: 2
    Last Post: 13th February 2006, 15:42

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.