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. QSharedPointer::data() as SIGNAL/SLOT parameter
Forum Updated to NodeBB v4.3 + New Features

QSharedPointer::data() as SIGNAL/SLOT parameter

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.8k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I want to create a QSharedPointer in one class and submit the data as a SIGNAL parameter:

    @emit mySignal(<QSharedPointer<MyClass>(new MyClass).data());@

    Then I have a number of SLOTS connected to this SIGNAL. However, when I try to access the underlying data from the SLOT, it seems that the data was deleted.

    So my question is: will QSharedPointer keep reference count if I emit the underlying data, or do I have to emit the QSharedPointer as is (see example below)?

    @emit mySignal(<QSharedPointer<MyClass>(new MyClass));@

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

      In your first emit you are just sending a pointer to the data the shared pointer is referencing. The shared pointer has no way to count the references to this. When the data will be released will be dependent on the way of your connect. In a syncronous connect, it will probably work.

      You have to use the second version of your emit.

      BTW: you have an extra '<' left of your QSharedPointer.

      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
        #3

        As soon as you touch the raw data, you're on your own in terms for ref counting. data() is a way to pass your class pointer to methods that require a raw pointer, but it is up to you to determine if that is safe for your case. In general, it breaks the concept of having a shared pointer at all. For example, using data() for QObject::connect() would be save, but normally methods that will store the pointer you pass in will not be save. If the method will "take ownership", it is definately unsafe to pass in a raw pointer via data().

        In your case, I would pass around the shared pointer via your signal if you need that. Why create a temporary shared pointer and then take the data member out of it again? It will just make sure that the instance is deleted as soon as the emit call finishes and the temporary goes out of scope. Would it not be possible to create a real value class that can safely be copied and does not need to be on the heap? It seems that that is what you are trying to achieve anyway: signal something that will be safely deleted if noone is interested in the signal value, right?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goocreations
          wrote on last edited by
          #4

          Thank you! I'll do that.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #5

            Yes, I've just created a "short version" of what I actually want to do (otherwise the post will be cluttered). So I will actually create it on the stack.

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

              Well, if you create the value to emit on the stack, you certainly don't want to pass around a shared pointer to it. What happens if code receiving the signal stores the shared pointer for later access, and then the original stack value goes out of scope? You'll have a dangling pointer inside your shared pointer!

              Shared pointers basically give value semantics to heap object pointers. If you already have a value object, there is no need to add such a wrapper (and in fact, as demonstrated above, it is dangerous to do so).

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goocreations
                wrote on last edited by
                #7

                Thanks Andre. Got a bit confused.

                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