Results 1 to 6 of 6

Thread: Qt smart pointer dilema

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Qt smart pointer dilema

    Hi Forum,

    Suppose that i define the following class as the subclass of QObject for smarter memory management:

    Qt Code:
    1. class Shader : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. .....................
    6.  
    7. .....................
    8. };
    To copy to clipboard, switch view to plain text mode 

    Then inside the another class definition i declare a pointer of the above type (composite pattern) as follows:

    Qt Code:
    1. Shader *shade;
    To copy to clipboard, switch view to plain text mode 

    And inside the source of the class definition i allocate memory on the heap. Just because i inherited from QObject , will the memory pointed by shade be de-allocated by Qt ?

    If this is so , then what is the point of having the following declaration instead?:

    Qt Code:
    1. QPointer<Shader> shade;
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QSharedPointer<Shader> shade;
    To copy to clipboard, switch view to plain text mode 


    or

    Qt Code:
    1. QScopedPointer<Shader> shade;
    To copy to clipboard, switch view to plain text mode 


    There are few more in the documentation. Lets understand the above first.

    Some explanation with code snippet would be very helpful.

    Thanks

  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: Qt smart pointer dilema

    Quote Originally Posted by sajis997 View Post
    Just because i inherited from QObject , will the memory pointed by shade be de-allocated by Qt ?
    It will be deallocated provided that the object being deleted is set as the parent of the Shader instance. However if you allocate memory in Shader itself without setting Shader as the parent object, you will have to free that allocated memory in the destructor (or elsewhere).

    Qt Code:
    1. class Shader : public QObject {
    2. Q_OBJECT
    3. public:
    4. Shader(QObject *parent = 0) : QObject(parent) {
    5. o1 = new QObject(this);
    6. o2 = new QObject;
    7. }
    8. ~Shader() { qDebug() << "Deleting" << objectName(); }
    9. private:
    10. QObject *o1;
    11. QObject *o2;
    12. };
    13.  
    14. void someFun() {
    15. QObject p; // allocated on the stack, will be deleted when the function returns
    16. Shader *shader = new Shader(&p);
    17. shader->setObjectName("Shader1");
    18. Shader *shader2 = new Shader; // no parent set
    19. shader2->setObjectName("Shader2");
    20. }
    To copy to clipboard, switch view to plain text mode 

    Shader1 will be deleted, Shader2 will not. Following the same rule "o1" will be deleted, "o2" will not.
    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. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt smart pointer dilema

    Also, in the above usages of smart pointers:

    QPointer does not delete the object, its purpose is to track the life cycle of a QObject based instance, i.e. reset itself to 0 if something deletes the object it points to.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qt smart pointer dilema

    Which type of qt pointer will delete the object as well once goes out of scope or when the application terminates abruptly ?

  5. #5
    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: Qt smart pointer dilema

    When the application terminates then it terminates, nothing will delete it.
    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. #6
    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: Qt smart pointer dilema

    QScopedPointer or QSharedPointer depending on your desired usage. These are close analogues of std::auto_ptr and std::shared_ptr

    Which destructors get called in the event of abnormal termination of a C++ program is dependent on a number of factors. All memory allocated to a program will typically be reclaimed by the operating system but other cleanup may not occur.

Similar Threads

  1. qt smart pointer
    By sajis997 in forum Newbie
    Replies: 4
    Last Post: 12th April 2011, 21:59
  2. Smart Pointer Design
    By lexfridman in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2011, 01:41
  3. smart pointers in Qt and Boost
    By pospiech in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2010, 14:24
  4. Qt 4.6: Which Qt smart pointer to use for this case?
    By ShaChris23 in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 03:11
  5. Replies: 1
    Last Post: 8th November 2009, 12:49

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.