Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Smart pointers in Qt
Forum Updated to NodeBB v4.3 + New Features

Smart pointers in Qt

Scheduled Pinned Locked Moved C++ Gurus
34 Posts 12 Posters 30.3k Views 1 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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #11

    [quote author="Duck" date="1309378118"]The question is rather "why would you want to use smart pointers if plain pointers are completely sufficient". Smart pointers do not come for free, both time- and space-wise. Some operations are as quick/as small as for plain pointers, some are significantly more expensive. No need to pay that cost if there is no gain.
    [/quote]

    The expence of smart pointers is certainly a reason to think twice.

    [quote author="Andre" date="1309423297"]
    Smart pointers are not always a good idea to use, but IMHO, they often are.[/quote]
    Do you have an example?
    Certainly it is not always the first choice to use pointers respectively smart pointers as already discussed above. I am wondering, if there are further situations where they are really not a good choise?

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #12

      [quote author="koahnig" date="1309424953"]
      [quote author="Andre" date="1309423297"]
      Smart pointers are not always a good idea to use, but IMHO, they often are.[/quote]
      Do you have an example?
      Certainly it is not always the first choice to use pointers respectively smart pointers as already discussed above. I am wondering, if there are further situations where they are really not a good choise?
      [/quote]

      One example may be the case where you store lots of pointers to objects in a container class. But, it all depends on your use case.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #13

        If you are in a time critical, real time issue, the indirection you get and perhaps the additional things to do, may hurt, as they cost time.

        But in mobile and desktop apps, I think, it does not hurt.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #14

          [quote author="Andre" date="1309426243"]
          One example may be the case where you store lots of pointers to objects in a container class. But, it all depends on your use case.
          [/quote]

          That is a good example to be careful with smart pointers. If somehow the object/container survives so does the smart pointer and the allocated memory.
          So a conclusion would be:
          watch out for run-away containers ;-)

          [quote author="Gerolf" date="1309426319"]
          But in mobile and desktop apps, I think, it does not hurt.[/quote]

          Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #15

            [quote author="koahnig" date="1309429658"]

            Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)
            [/quote]
            Off topic:
            See "here":http://www.google.nl/search?hl=nl&xhr=t&q=lake+aral&cp=0&nord=1&bav=on.2,or.r_gc.r_pw.&biw=1280&bih=963&ion=1&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi for how such a thing looks...

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #16

              [quote author="koahnig" date="1309429658"]
              [quote author="Gerolf" date="1309426319"]
              But in mobile and desktop apps, I think, it does not hurt.[/quote]

              Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)

              [/quote]

              But itÄs the same with pure pointers. You have to watch out to delete allocated memory. No chance to get around this problem, this or that way.

              Where I love smart pointers is if you have some function that needs to allocate memory to read some values from Windows API where yxou have to pre allocate the memory. Just use some auto pointers and it will be destructed correctly when you leave the function, even in exception handling cases.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #17

                [quote author="Gerolf" date="1309430523"]
                But itÄs the same with pure pointers. You have to watch out to delete allocated memory. No chance to get around this problem, this or that way.

                Where I love smart pointers is if you have some function that needs to allocate memory to read some values from Windows API where yxou have to pre allocate the memory. Just use some auto pointers and it will be destructed correctly when you leave the function, even in exception handling cases.[/quote]

                Yes! However, one should be aware that auto_ptr does not mean automatically no problem at all.
                I am certainly in favor of shared_ptr .

                [quote author="Andre" date="1309430218"]
                Off topic:
                See "here":http://www.google.nl/search?hl=nl&xhr=t&q=lake+aral&cp=0&nord=1&bav=on.2,or.r_gc.r_pw.&biw=1280&bih=963&ion=1&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi for how such a thing looks...
                [/quote]

                :-D A good example !

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Duck
                  wrote on last edited by
                  #18

                  [quote author="Gerolf" date="1309414170"]
                  It depends what you do if plain pointer are sufficient. YOu can surely always use plain pointers and do everything on your own, but why should you do that?
                  [/quote]

                  Because they are cheap and fast.

                  [quote author="Gerolf" date="1309414170"]
                  E.g. shared pointers: The objects are deleted, if the last shared pointer goes out of scope. If you do that with plain pointers, you have to implement ref counting etc. And there are many more types of shared pointers, e.g. QPOinter which goes to 0 if the object is destroyed from somewhere etc.

                  From my opinion, smart pointer are nearly always good to use![/quote]

                  From my point of view it's pretty much the opposite. Shared pointer impose overhead in lots of cases, so using them without justification violates the "don't pay if you don't need it" principle.

                  Often enough there appears to be a "need to use SharedPointers" to "fix" "issues" that arise typically from a design that does not have a clear concept of ownership.

                  Fortunately enough, in a QObject based world that's rather rare as most QObjects do have an owner. No need for any "manual" management here either.

                  Shared

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gbonnema
                    wrote on last edited by
                    #19

                    Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
                    Specifically, does it render the Qt smart pointers obsolete?

                    P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

                    My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

                    Anyone comments?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gbonnema
                      wrote on last edited by
                      #20

                      Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
                      Specifically, does it render the Qt smart pointers obsolete?

                      P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

                      My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

                      Anyone comments?

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #21

                        Hi,

                        [quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
                        Specifically, does it render the Qt smart pointers obsolete?

                        P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

                        My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

                        Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available

                        Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.

                        Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.

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

                        1 Reply Last reply
                        0
                        • JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #22

                          Hi,

                          [quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
                          Specifically, does it render the Qt smart pointers obsolete?

                          P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

                          My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

                          Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available

                          Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.

                          Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.

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

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sandy.martel
                            wrote on last edited by
                            #23

                            bq. std::shared_ptr doesn’t have this ability.

                            yes it does, see std::dynamic_pointer_cast<>().

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sandy.martel
                              wrote on last edited by
                              #24

                              bq. std::shared_ptr doesn’t have this ability.

                              yes it does, see std::dynamic_pointer_cast<>().

                              1 Reply Last reply
                              0
                              • JKSHJ Offline
                                JKSHJ Offline
                                JKSH
                                Moderators
                                wrote on last edited by
                                #25

                                [quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.

                                yes it does, see std::dynamic_pointer_cast<>().
                                [/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::​dynamicCast(),. It relies on RTTI.

                                I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).

                                QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don't.

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

                                1 Reply Last reply
                                0
                                • JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #26

                                  [quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.

                                  yes it does, see std::dynamic_pointer_cast<>().
                                  [/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::​dynamicCast(),. It relies on RTTI.

                                  I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).

                                  QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don't.

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

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    sandy.martel
                                    wrote on last edited by
                                    #27

                                    bq. I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).

                                    dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.

                                    bq. QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don’t.

                                    I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      sandy.martel
                                      wrote on last edited by
                                      #28

                                      bq. I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).

                                      dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.

                                      bq. QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don’t.

                                      I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.

                                      1 Reply Last reply
                                      0
                                      • JKSHJ Offline
                                        JKSHJ Offline
                                        JKSH
                                        Moderators
                                        wrote on last edited by
                                        #29

                                        [quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.

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

                                        1 Reply Last reply
                                        0
                                        • JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by
                                          #30

                                          [quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.

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

                                          1 Reply Last reply
                                          0

                                          • Login

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