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. How QSharedPointer works?
Forum Updated to NodeBB v4.3 + New Features

How QSharedPointer works?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.0k 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.
  • S Offline
    S Offline
    Szymon. M. Sabat
    wrote on last edited by
    #1

    I have QList<QSharedPointer<MyClass>>. I use elements of the list by creating a new (not a keyword) QSharedPointer<MyClass> newPointer = list.at(x); Then I pass it around and do the work, then the pointer dies but I have an extra one in the list so everything's fine, right? I noticed the destructor of MyClass is called after I leave the pointer in the list for a while, I don't understand why or when exactly... then I'm finding QSharedPointer inside the list but raising exception on list.at(x).data(); Researching the issue I found this: "[Pointer tracking] allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects." - how is that a mistake? Why would I need a smart pointer like that if not to have many copies of it and expect the data to be removed as last of them goes out of scope? Do I completely misunderstood how QSharedPointer work and what problem are they solving?

    W 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QSharedPointer deletes (as std::shared_ptr) the object when it is no longer referenced. So what you're telling looks some kind of strange. Therefore please show us some code or even better - write a small testcase to reproduce the problem.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      1
      • S Szymon. M. Sabat

        I have QList<QSharedPointer<MyClass>>. I use elements of the list by creating a new (not a keyword) QSharedPointer<MyClass> newPointer = list.at(x); Then I pass it around and do the work, then the pointer dies but I have an extra one in the list so everything's fine, right? I noticed the destructor of MyClass is called after I leave the pointer in the list for a while, I don't understand why or when exactly... then I'm finding QSharedPointer inside the list but raising exception on list.at(x).data(); Researching the issue I found this: "[Pointer tracking] allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects." - how is that a mistake? Why would I need a smart pointer like that if not to have many copies of it and expect the data to be removed as last of them goes out of scope? Do I completely misunderstood how QSharedPointer work and what problem are they solving?

        W Offline
        W Offline
        wrosecrans
        wrote on last edited by
        #3

        @Szymon-M-Sabat said in How QSharedPointer works?:

        "[Pointer tracking] allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects." - how is that a mistake?

        int *my_pointer = new int;
        some_shared_pointer foo(my_pointer);
        some_shared_pointer bar(my_pointer);
        

        Foo and Bar might look like they are sharing with each other, but they aren't! Bar has no idea that foo already thinks it's responsible for the pointer. Because bar wasn't specifically made from foo using the right copying API, it thinks it was created from scratch, and it owns the pointer all by itself. Subsequent shared pointers made from either foo or bar will be in separate little ref counting universes, and when either foo or bar thinks that the ref count is zero, it'll try to delete my_pointer. Even if the other refcount thinks there are a thousand objects still using it.

        int *my_pointer = new int;
        some_shared_pointer foo(my_pointer);
        some_shared_pointer bar(foo);
        

        This time, bar is made from foo instead of from the original bare pointer, so it shares the refcount, and won't explode unpredictably. This is what your original quote was talking about. Having another shared pointer ultimately derived from the same address is good and correct. Having two shared pointers directly assigned authority over the actual address will result in them fighting, and your application will be caught in the crossfire. The obvious followon question is couldn't somebody make a smart pointer class that uses some sort of clever global registry of addresses? And the answer is only-sorta, but the performance implications would be horrific for something intended to be quite efficient, so you should solve the ownership problem elsewhere.

        Use the debugger to set a breakpoint in your class destructor to figure out who is actually deleting it to get a better understanding of what's currently happening..

        1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          QSharedPointer deletes (as std::shared_ptr) the object when it is no longer referenced. So what you're telling looks some kind of strange. Therefore please show us some code or even better - write a small testcase to reproduce the problem.

          S Offline
          S Offline
          Szymon. M. Sabat
          wrote on last edited by Szymon. M. Sabat
          #4

          @Christian-Ehrlicher okay, so I've found out what I did wrong. I was getting a raw pointer by other means (signal-slot mechanism and the sender() function) and I constructed QSharedPointer based on that raw pointer so the other pointer didn't know about it (as I understand it). I needed to use the raw pointer to iterate over my list to find the original QSharedPointer and build the new one using it. Marking as solved, thanks for clarifying that the pointers work as I understood they should.

          @wrosecrans wrote exactly what happened. Thank you! I think the docs could include your description.

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Szymon-M-Sabat said in How QSharedPointer works?:

            I think the docs could include your description.

            It is - see https://doc.qt.io/qt-5/qsharedpointer.html#QSharedPointer-1 :

            The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @Szymon-M-Sabat said in How QSharedPointer works?:

              I think the docs could include your description.

              It is - see https://doc.qt.io/qt-5/qsharedpointer.html#QSharedPointer-1 :

              The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.

              S Offline
              S Offline
              Szymon. M. Sabat
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in How QSharedPointer works?:

              The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.

              Yeah, now I get it but it wasn't obvious for me then, I was like "but I want to have many QSharedPointers" :(

              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