Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Raw pointers in Qt
Forum Updated to NodeBB v4.3 + New Features

Raw pointers in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • YaxezeY Offline
    YaxezeY Offline
    Yaxeze
    wrote on last edited by
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • YaxezeY Yaxeze

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      YaxezeY 1 Reply Last reply
      0
      • JKSHJ JKSH

        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

        YaxezeY Offline
        YaxezeY Offline
        Yaxeze
        wrote on last edited by
        #3

        @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.

        kshegunovK JKSHJ 2 Replies Last reply
        0
        • YaxezeY Yaxeze

          @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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • YaxezeY Yaxeze

            @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.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @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.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved