Raw pointers in Qt



  • I was wondering if it's safe to use raw pointers in Qt, I mean, Qt has the QObject that owns its children and deletes them automatically, is it wrong to use raw pointers in Qt? I mean, I have a classe called Foo that inherits QObject and inside that class, in the private section, I have Foo *foo. Is it wrong? Will this leak at any point?
    When should I use smart pointers in Qt?


  • Moderators

    Hi,

    @Yaxeze said:

    Is it wrong? Will this leak at any point?

    It is not wrong. You just need to manage your memory carefully. If you do that, then it won't leak.

    When should I use smart pointers in Qt?

    There are many kinds of smart pointers. Understand their differences first: http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available



  • @JKSH said:

    You just need to manage your memory carefully.

    Sorry, but it's a really vague answer. I still don't get it. If I do what I said above, will it leak? or QObject will delete for me?
    And I kinda know the differences between the smart pointers, but I was wondering when to use in Qt, as it has QObject to handle the deletions and stuff, I was thinking if it was really necessary to use smart pointers.


  • Qt Champions 2016

    @Yaxeze

    These two threads should be helpfull (and hopfully sufficient) to answer your questions.

    http://forum.qt.io/topic/63723/custom-class-qobject-inheritance
    http://forum.qt.io/topic/61842/how-can-i-ensure-proper-resource-management-raii-when-using-qt-plugins

    And this page from the documentation as well:
    http://doc.qt.io/qt-5/objecttrees.html


  • Moderators

    @Yaxeze said:

    Sorry, but it's a really vague answer. I still don't get it. If I do what I said above, will it leak? or QObject will delete for me?

    You did not describe how you create your Foo object and how you assign it to the foo variable, so we cannot see whether you have a leak or not.

    Read the links that @kshegunov posted for details.

    Here is a summary:

    class MyObject : public QObject
    {
        Q_OBJECT
    
    public:
        MyObject(QObject *parent = nullptr) {
    		obj1 = new QObject(this); // Sets the parent using the 'parent' constructor parameter
    		
    		obj2 = new QObject;
    		obj2->setParent(this); // Sets the parent using QObject::setParent()
    		
    		obj3 = new QObject; // No parent
    	}
    
    private:
        QObject *obj1;
    	QObject *obj2;
    	QObject *obj3;
    };
    

    When you delete your MyObject, it will automatically delete obj1 and obj2 because of the parent-child relationship. However, obj3 will not get deleted because you did not set it as a child, so the memory for obj3 gets leaked.

    And I kinda know the differences between the smart pointers, but I was wondering when to use in Qt, as it has QObject to handle the deletions and stuff, I was thinking if it was really necessary to use smart pointers.

    There are many ways to handle deletions: (i) do it manually, (ii) use smart pointers, (iii) use QObject's parent-child system. You can choose which one you want to use.

    Personally, I don't use smart pointers with QObjects.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.