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. Can't pass a signal with QSharedPointer (or std::shared_ptr<>) between threads

Can't pass a signal with QSharedPointer (or std::shared_ptr<>) between threads

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 8.4k Views
  • 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.
  • michalosM Offline
    michalosM Offline
    michalos
    wrote on last edited by michalos
    #1

    Hi
    I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
    When I try to catch an empty signal, everything works fine.

    My code :

    //creating the QShared ptr
    //   std::shared_ptr<Message> msgShared (new Message());
    
        QSharedPointer<Message> message(new Message());
    
    // emiting : (works)
                emit parsingTextMessageFinished(message);
    
    //different class:
    
     void PBXReader::initHandlers(){
     smsHandler = new SMSTagHandler(req_ans_mangr, this);
    
      connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler);
    
    }
    
    void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg)
    {
        emit parsingTextMessageFinShared(msg);
        emit parsingTextMessageFin();
    
    }
    

    and in the main thread:

    public slots:
        void gotParsingTextMessage(QSharedPointer<Message>);
        void gotParsingTextMesg();
    
        connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg);
        connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
    

    Does anyone got any idea why?

    mrjjM 1 Reply Last reply
    0
    • michalosM michalos

      Hi
      I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
      When I try to catch an empty signal, everything works fine.

      My code :

      //creating the QShared ptr
      //   std::shared_ptr<Message> msgShared (new Message());
      
          QSharedPointer<Message> message(new Message());
      
      // emiting : (works)
                  emit parsingTextMessageFinished(message);
      
      //different class:
      
       void PBXReader::initHandlers(){
       smsHandler = new SMSTagHandler(req_ans_mangr, this);
      
        connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler);
      
      }
      
      void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg)
      {
          emit parsingTextMessageFinShared(msg);
          emit parsingTextMessageFin();
      
      }
      

      and in the main thread:

      public slots:
          void gotParsingTextMessage(QSharedPointer<Message>);
          void gotParsingTextMesg();
      
          connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg);
          connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
      

      Does anyone got any idea why?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @michalos

      Hi normally
      Signals between threads is Qt::QueuedConnection
      and you must use type that is known how to queue.

      I wonder if QSharedPointer<Message>
      is seens as new type.

      You dont get any warnings ?

      1 Reply Last reply
      3
      • michalosM Offline
        michalosM Offline
        michalos
        wrote on last edited by
        #3

        @mrjj
        Thank You for Your answer.
        I was wondering the same thing.
        I don't get any info, that I should register a metatype (qRegisterMetaType).
        I do not get any warnings nor debug info.
        When I use a C style pointer (as below) everything works fine.

            Message *message = new Message(this);
        
        mrjjM 1 Reply Last reply
        0
        • michalosM michalos

          @mrjj
          Thank You for Your answer.
          I was wondering the same thing.
          I don't get any info, that I should register a metatype (qRegisterMetaType).
          I do not get any warnings nor debug info.
          When I use a C style pointer (as below) everything works fine.

              Message *message = new Message(this);
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @michalos
          Yes it was the qRegisterMetaType i was thinking of.
          But it seems you are fully aware of this and would see any warning so its not htat.

          Next question is.

          Does it go out of scope? ( for some reason)

          1 Reply Last reply
          0
          • michalosM Offline
            michalosM Offline
            michalos
            wrote on last edited by michalos
            #5

            @mrjj
            That is a good question.
            I do not know if it's out of scope.

            If I create it like that:

            QSharedPointer<Message> message(new Message());
            

            or

            std::shared_ptr<Message> msgShared (new Message());
            

            will it go out of scope?
            Isn't it created on the heap?

            mrjjM 1 Reply Last reply
            0
            • michalosM michalos

              @mrjj
              That is a good question.
              I do not know if it's out of scope.

              If I create it like that:

              QSharedPointer<Message> message(new Message());
              

              or

              std::shared_ptr<Message> msgShared (new Message());
              

              will it go out of scope?
              Isn't it created on the heap?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I'm not sure. ( its with new so ..)

              Is Message your own class?

              I wonder if you can put aDebug() or breakpoint in the destructor and see if it dies when copied..

              Also make sure you have Qt::QueuedConnection on your connects

              1 Reply Last reply
              3
              • michalosM Offline
                michalosM Offline
                michalos
                wrote on last edited by
                #7

                I've changed the connection to Qt::QueuedConnection and it worked :)

                I thought that this is done automatically when connecting between threads, but in this case it didn't.

                Thank You for Your help :)

                mrjjM 1 Reply Last reply
                0
                • michalosM michalos

                  I've changed the connection to Qt::QueuedConnection and it worked :)

                  I thought that this is done automatically when connecting between threads, but in this case it didn't.

                  Thank You for Your help :)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @michalos
                  Oh. super.
                  I do wonder how it could work with no QueuedConnection and
                  no QSharedPointer but I guess its just random :)

                  1 Reply Last reply
                  0
                  • michalosM Offline
                    michalosM Offline
                    michalos
                    wrote on last edited by
                    #9

                    I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.

                    mrjjM 1 Reply Last reply
                    0
                    • michalosM michalos

                      I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @michalos

                      Well, seems its all explained here
                      http://doc.qt.io/qt-5/threads-qobject.html
                      I guess that Auto Connection does guess. :)

                      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