Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-2842

QSignalSpy crashes if signal is emitted from worker thread

    XMLWordPrintable

Details

    Description

      QSignalSpy enforces a Qt::DirectConnection, which makes a valid test case like below crash.

      #include <QtGui>
      #include <QtTest/QtTest>
      
      class Emitter : public QObject
      {
          Q_OBJECT
      public:
          Emitter()
          {
          }
      
          void emitSignal()
          {
              emit signal();
          }
      signals:
          void signal();
      };
      
      class MyThread : public QThread
      {
          Q_OBJECT
      public:
          MyThread(Emitter *emitter)
              : m_emitter(emitter)
          {
          }
      
          ~MyThread()
          {
              wait(100);
          }
      
      protected:
          void run()
          {
              for (int i = 0; i < 100; ++i) {
                  m_emitter->emitSignal();
                  msleep(100);
              }
          }
      
      private:
          Emitter *m_emitter;
      };
      
      #include "main.moc"
      
      int main( int argc, char *argv[] )
      {
          QApplication app( argc, argv );
      
          Emitter emitter;
          QSignalSpy spy(&emitter, SIGNAL(signal()));
          for (int t = 0; t < 5; ++t) {
              MyThread *thread = new MyThread(&emitter);
              thread->start();
              QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          }
          QTest::qWait(20000);
      
          qDebug() << spy.count();
      
          return 0;
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            macadder Jason McDonald
            vhilshei Volker Hilsheimer
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes