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. Any difference between std::shared_ptr and QSharedPointer?
Forum Updated to NodeBB v4.3 + New Features

Any difference between std::shared_ptr and QSharedPointer?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 22.2k 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.
  • B Offline
    B Offline
    bmanc
    wrote on 14 Aug 2012, 17:28 last edited by
    #1

    Is there a difference between std::shared_ptr (now in C++ 11) and QSharedPointer?

    I was wondering about this because I was hoping that maybe QSharedPointer understands QObjects, which std::shared_ptr obviously wouldn't. I'm specifically interested in the scenario where I have an instance of QSharedPointer<SomeClass> where SomeClass is derived from QObject and has a QObject parent. If the parent is deleted, then its child should be deleted. What happens to the QSharedPointer instance in that case? I believe std::shared_ptr would be invalid at that point.

    Thanks

    bmanc

    1 Reply Last reply
    1
    • E Offline
      E Offline
      elmigranto
      wrote on 14 Aug 2012, 17:55 last edited by
      #2

      See here — http://stackoverflow.com/a/5026705/704503

      Qt's containers and other classes similar to STL's ones are:

      • aware of Qt architecture (perfomance gains and other “bonuses”);
      • designed to replace STL on platforms where it's realization not available;
      • a gurantee that there are no diferrences in plenty of realizations.

      I personally believe, that you should use Qt's ones in Qt Project. Not a big deal though, if you will decide to switch to STL until your code is consistant (or you know exactly what are you doing and why).

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bmanc
        wrote on 14 Aug 2012, 21:45 last edited by
        #3

        That's a nice list. Thanks for the link. I'm still wondering though how does QSharedPointer interact with a QObject?

        1 Reply Last reply
        0
        • E Offline
          E Offline
          elmigranto
          wrote on 15 Aug 2012, 04:51 last edited by
          #4

          [quote author="bmanc" date="1344980719"]I'm still wondering though how does QSharedPointer interact with a QObject?[/quote]
          What do you mean? If you think “understanding of QObject” is removing all of it's children, you're are wrong, because this is part of QObject itself (here's quite alright video on topic — "http://qt-project.org/videos/watch/fundamentals-of-qt-objects-in-qt-part-1-3":http://qt-project.org/videos/watch/fundamentals-of-qt-objects-in-qt-part-1-3).

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bmanc
            wrote on 22 Aug 2012, 19:43 last edited by
            #5

            Sorry, you misunderstood the statement. What I mean is I don't want QSharedPointer to remove a QObject since it's QObject parent will - provided it has a parent. If QSharedPointer understood the QObject hierarchy, then I was hoping it would be smart enough to figure out whether the pointed-to QObject has a parent or not, and handle destruction appropriately.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              elmigranto
              wrote on 23 Aug 2012, 03:58 last edited by
              #6

              QSharedPointer will do nothing else except delete (no matter what's encapsulated), unless you explicitly specify something other (Deleter).

              During delete QObject, it will be disconnected from parent. You can, however, write custom deleter as described in "documentation":http://qt-project.org/doc/qt-4.8/qsharedpointer.html#QSharedPointer-3

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bmanc
                wrote on 23 Aug 2012, 04:25 last edited by
                #7

                So to answer the original question, it's no different than std::shared_ptr. That's what I would do with the shared_ptr too.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  elmigranto
                  wrote on 6 Oct 2012, 18:42 last edited by
                  #8

                  I've faced somehow similar problem and the answer seems to be "QObjectCleanupHandler":http://qt-project.org/doc/qt-4.8/qobjectcleanuphandler.html

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on 6 Oct 2012, 20:34 last edited by
                    #9

                    [quote author="bmanc" date="1344965308"]Is there a difference between std::shared_ptr (now in C++ 11) and QSharedPointer?

                    I was wondering about this because I was hoping that maybe QSharedPointer understands QObjects, which std::shared_ptr obviously wouldn't. I'm specifically interested in the scenario where I have an instance of QSharedPointer<SomeClass> where SomeClass is derived from QObject and has a QObject parent. If the parent is deleted, then its child should be deleted. What happens to the QSharedPointer instance in that case? I believe std::shared_ptr would be invalid at that point.
                    [/quote]

                    Storing a QObject with a parent inside a shared pointer is violating the contract you have with the shared pointer class: it's the only one managing the lifetime of the pointee. In other words, it's perfecty equivalent of doing

                    @
                    Object *o = new Object;
                    SharedPointer<Object> sp(o);
                    delete o;
                    @

                    This will incur in a double deletion when the shared pointer's refcount drops to zero, and you'll dereference a dangling pointer if you access the pointer stored in the shared pointer after that the delete has happened.

                    But IMHO the point is: if you already have a QObject with a parent, you're already managing its lifetime. With a shared pointer you're managing it twice, which will cause all sort of troubles. Are you really sure about your design?

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AaronRidout
                      wrote on 29 Nov 2017, 14:35 last edited by
                      #10

                      I have just found out that the QVector copy constructor (& move constructor) is very different from that of a std::vector! The QVector implements a variation on the COW pattern, whilest std::vector is a deep copy of the object. Here be dragons!
                      Compare the Qt description http://doc.qt.io/qt-5/qvector.html#QVector-3 versus http://www.cplusplus.com/reference/vector/vector/vector/

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 29 Nov 2017, 22:47 last edited by
                        #11

                        Hi,

                        All Qt containers implement COW (Copy On Write).

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        • Login

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