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. Simplest app with QThread crashes on exit
Forum Updated to NodeBB v4.3 + New Features

Simplest app with QThread crashes on exit

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.5k Views 2 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.
  • A Offline
    A Offline
    Amir Afendin
    wrote on last edited by
    #1
    #include <QCoreApplication>
    #include <QThread>
    #include <QDebug>
    
    class First : public QObject {
        Q_OBJECT
    public slots:
        void callSlot() { qDebug() << "First slot called from thread ";}
    };
    
    class Second : public QObject {
        Q_OBJECT
    
        First first;
        QThread thread;
    signals:
        void callSignal();
    
    public:
        Second() {
            first.moveToThread(&thread);
            connect(this, &Second::callSignal, &first, &First::callSlot);
            thread.start();
        }
    
        ~Second() {
            thread.quit();
            thread.wait();
        }
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Second second;
        emit second.callSignal();
    
        return a.exec();
    }
    
    #include "main.moc"
    

    Why should this app crash? Implementation doesn't even differ from what docs show here

    JKSHJ kshegunovK 2 Replies Last reply
    0
    • A Amir Afendin
      #include <QCoreApplication>
      #include <QThread>
      #include <QDebug>
      
      class First : public QObject {
          Q_OBJECT
      public slots:
          void callSlot() { qDebug() << "First slot called from thread ";}
      };
      
      class Second : public QObject {
          Q_OBJECT
      
          First first;
          QThread thread;
      signals:
          void callSignal();
      
      public:
          Second() {
              first.moveToThread(&thread);
              connect(this, &Second::callSignal, &first, &First::callSlot);
              thread.start();
          }
      
          ~Second() {
              thread.quit();
              thread.wait();
          }
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Second second;
          emit second.callSignal();
      
          return a.exec();
      }
      
      #include "main.moc"
      

      Why should this app crash? Implementation doesn't even differ from what docs show here

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Amir-Afendin said in Simplest app with QThread crashes on exit:

      Why should this app crash?

      Run your app in Debug mode.

      What does your crash stack trace say?

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

      A 1 Reply Last reply
      3
      • A Amir Afendin
        #include <QCoreApplication>
        #include <QThread>
        #include <QDebug>
        
        class First : public QObject {
            Q_OBJECT
        public slots:
            void callSlot() { qDebug() << "First slot called from thread ";}
        };
        
        class Second : public QObject {
            Q_OBJECT
        
            First first;
            QThread thread;
        signals:
            void callSignal();
        
        public:
            Second() {
                first.moveToThread(&thread);
                connect(this, &Second::callSignal, &first, &First::callSlot);
                thread.start();
            }
        
            ~Second() {
                thread.quit();
                thread.wait();
            }
        };
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            Second second;
            emit second.callSignal();
        
            return a.exec();
        }
        
        #include "main.moc"
        

        Why should this app crash? Implementation doesn't even differ from what docs show here

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @Amir-Afendin said in Simplest app with QThread crashes on exit:

        Implementation doesn't even differ from what docs show here

        It does slightly. The worker object in the docs is allocated in the heap. By looking at the code it crashes in the destructor of the worker object (in QObject::~QObject) where there is some logic that references the current object thread. Since the thread has already been destroyed by the stack unwinding you'd get a dangling pointer.

        In conclusion, what @JKSH said:
        What does your stack trace say?

        PS.
        I expect that exchanging the two member declarations would fix it. That is:

            // ...
            QThread thread;
            First first;
        

        Read and abide by the Qt Code of Conduct

        A 1 Reply Last reply
        2
        • JKSHJ JKSH

          @Amir-Afendin said in Simplest app with QThread crashes on exit:

          Why should this app crash?

          Run your app in Debug mode.

          What does your crash stack trace say?

          A Offline
          A Offline
          Amir Afendin
          wrote on last edited by
          #4

          @JKSH
          861f0b2c-7f29-4c67-9f6d-c9de6214f173-image.png

          c4aa7bfe-aeed-4c88-96b2-6d393adf0b13-image.png

          1 Reply Last reply
          0
          • kshegunovK kshegunov

            @Amir-Afendin said in Simplest app with QThread crashes on exit:

            Implementation doesn't even differ from what docs show here

            It does slightly. The worker object in the docs is allocated in the heap. By looking at the code it crashes in the destructor of the worker object (in QObject::~QObject) where there is some logic that references the current object thread. Since the thread has already been destroyed by the stack unwinding you'd get a dangling pointer.

            In conclusion, what @JKSH said:
            What does your stack trace say?

            PS.
            I expect that exchanging the two member declarations would fix it. That is:

                // ...
                QThread thread;
                First first;
            
            A Offline
            A Offline
            Amir Afendin
            wrote on last edited by
            #5

            @kshegunov
            Screenshots above were made with app version where I allocate worker int the heap.

            kshegunovK 1 Reply Last reply
            0
            • A Amir Afendin

              @kshegunov
              Screenshots above were made with app version where I allocate worker int the heap.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              This doesn't look like an active trace. Take the backtrace after the crash signals the debugger and stops the application from running.

              Read and abide by the Qt Code of Conduct

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

                Your testcase works fine for me (only had to add a line to exit the app - QTimer::singleShot(2000, qApp, QCoreApplication::quit); )

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

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  Amir Afendin
                  wrote on last edited by
                  #8

                  Looks like if you create empty "Qt Console Application" it crashes anyway. So there's no thread problem here I 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