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. Qt Application crashes :QThread: Destroyed while thread is still running

Qt Application crashes :QThread: Destroyed while thread is still running

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreator 5.0qthread threadsqthreadqtapplication
9 Posts 3 Posters 2.7k 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.
  • V Offline
    V Offline
    Vivek_A
    wrote on 24 Jan 2022, 06:27 last edited by Vivek_A
    #1

    Hi,
    i have seen lot of question related to this ,but nothing that help that much ..
    When i close my application , crashed pop up coming .. in debugger saying QThread: Destroyed while thread is still running.

    i referred link text,link text,stackoverflow

    so i created following in my main.cpp

    int main(int argc, char *argv[])
    {
       WorkerObject* a = new WorkerObject();
       RTLSControllerApplication app(argc, argv);
       app.mainWindow()->show();
       QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
       return app.QApplication::exec();
    }
    

    i created worker thread and its connected from RTLSClient.cpp class signal.
    in RTLSClient.cpp

    RTLSClient::~RTLSClient()
    {
        workerThread.quit();
        workerThread.wait();
    }
    

    still same problem !!
    For more details - this is a continution of my previous question Update Data to mongodb through Qthread..

    vk

    J 1 Reply Last reply 24 Jan 2022, 06:33
    0
    • V Vivek_A
      24 Jan 2022, 06:27

      Hi,
      i have seen lot of question related to this ,but nothing that help that much ..
      When i close my application , crashed pop up coming .. in debugger saying QThread: Destroyed while thread is still running.

      i referred link text,link text,stackoverflow

      so i created following in my main.cpp

      int main(int argc, char *argv[])
      {
         WorkerObject* a = new WorkerObject();
         RTLSControllerApplication app(argc, argv);
         app.mainWindow()->show();
         QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
         return app.QApplication::exec();
      }
      

      i created worker thread and its connected from RTLSClient.cpp class signal.
      in RTLSClient.cpp

      RTLSClient::~RTLSClient()
      {
          workerThread.quit();
          workerThread.wait();
      }
      

      still same problem !!
      For more details - this is a continution of my previous question Update Data to mongodb through Qthread..

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 24 Jan 2022, 06:33 last edited by
      #2

      @Vivek_A
      instead of
      QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));

      I think you actually want this connect:

      QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      V 1 Reply Last reply 24 Jan 2022, 09:39
      1
      • J J.Hilk
        24 Jan 2022, 06:33

        @Vivek_A
        instead of
        QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));

        I think you actually want this connect:

        QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
        
        V Offline
        V Offline
        Vivek_A
        wrote on 24 Jan 2022, 09:39 last edited by
        #3

        @J-Hilk sry , there is one doubt ,
        i created Qthread variable in RTLSClient.h file and moving worker object to this thread..

        QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
        

        This saying quit not a member of WorkerObject.
        so i created a slot (quit ) in WorkerObject for calling thread quit and wait .. here is my doubt Qthread variable is in RTLSClient class right .......??????

        vk

        J 1 Reply Last reply 24 Jan 2022, 10:45
        0
        • V Vivek_A
          24 Jan 2022, 09:39

          @J-Hilk sry , there is one doubt ,
          i created Qthread variable in RTLSClient.h file and moving worker object to this thread..

          QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
          

          This saying quit not a member of WorkerObject.
          so i created a slot (quit ) in WorkerObject for calling thread quit and wait .. here is my doubt Qthread variable is in RTLSClient class right .......??????

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 24 Jan 2022, 10:45 last edited by
          #4

          @Vivek_A
          you only gave me this to work with

          int main(int argc, char *argv[])
          {
             WorkerObject* a = new WorkerObject();
             RTLSControllerApplication app(argc, argv);
             app.mainWindow()->show();
             QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
             return app.QApplication::exec();
          }
          

          There is no mention of QThread instance, or an RTLSClient instance nor do I see the moveToThread call

          So my example code is based on guess work.

          You have to show some more context for me to adapt the example code,

          but, you're right, the signal aboutToQuit from your QCoreApplication (qApp) has to be connected to the slot quit of the QThread instance


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          V 1 Reply Last reply 25 Jan 2022, 07:09
          0
          • J J.Hilk
            24 Jan 2022, 10:45

            @Vivek_A
            you only gave me this to work with

            int main(int argc, char *argv[])
            {
               WorkerObject* a = new WorkerObject();
               RTLSControllerApplication app(argc, argv);
               app.mainWindow()->show();
               QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
               return app.QApplication::exec();
            }
            

            There is no mention of QThread instance, or an RTLSClient instance nor do I see the moveToThread call

            So my example code is based on guess work.

            You have to show some more context for me to adapt the example code,

            but, you're right, the signal aboutToQuit from your QCoreApplication (qApp) has to be connected to the slot quit of the QThread instance

            V Offline
            V Offline
            Vivek_A
            wrote on 25 Jan 2022, 07:09 last edited by Vivek_A
            #5

            @J-Hilk sorry for not detailing ,,,

            workerobject.h

            #include <QObject>
            #include <QThread>
            class WorkerObject : public QObject
            {
               Q_OBJECT
            
            public:
               explicit WorkerObject(QObject *parent = nullptr);
            
            public slots:
               void heavyOperation(int ,double ,double);
               void quit();
            signals:
               void finished();
            private:
            };
            

            RTLSClient.h

            class RTLSClient : public QObject
            {
               Q_OBJECT
                QThread workerThread;
                   public:
                       explicit RTLSClient(QObject *parent = 0);
                       virtual ~RTLSClient();
                   //and some signal and slots .......
            
               ***RTLSClient.cpp***
            

            RTLSClient.cpp

            WorkerObject *worker = new WorkerObject;
            worker->moveToThread(&workerThread);
            connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
            connect(this, &RTLSClient::sendtodb, worker, &WorkerObject::heavyOperation);
              connect(worker, &WorkerObject::finished, &workerThread, &QThread::quit);
               connect(worker, &WorkerObject::finished, worker, &QObject::deleteLater);
            
            RTLSClient::~RTLSClient()
            {
                
                workerThread.quit();
                workerThread.wait();
            
                if(file != nullptr)
                    file->close();
            }
            
            

            workersobject.cpp

            WorkerObject::WorkerObject(QObject *parent) : QObject(parent)
            {
            
            }
            
            void WorkerObject::heavyOperation(int a ,double x,double y)
            {
            dbx["Tag2"].delete_one(document{} << "bool" << 1 << finalize);
            }
            

            THESE are the main code segments iam make use of threading ...
            can you find what is the mistake and how to quit thread safely.

            vk

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 25 Jan 2022, 07:13 last edited by
              #6

              @Vivek_A said in Qt Application crashes :QThread: Destroyed while thread is still running:

              RTLSClient

              Where do you create/delete this object? If you don't delete it then the dtor will not be called and your thread will not be stopped.

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

              V 1 Reply Last reply 25 Jan 2022, 08:13
              3
              • Christian EhrlicherC Christian Ehrlicher
                25 Jan 2022, 07:13

                @Vivek_A said in Qt Application crashes :QThread: Destroyed while thread is still running:

                RTLSClient

                Where do you create/delete this object? If you don't delete it then the dtor will not be called and your thread will not be stopped.

                V Offline
                V Offline
                Vivek_A
                wrote on 25 Jan 2022, 08:13 last edited by Vivek_A
                #7

                @Christian-Ehrlicher its created in main thread-

                RTLSControllerApplication::RTLSControllerApplication(int &argc, char **argv) : QApplication(argc, argv)
                {
                  _cleConnection = new RTLSCLEConnection(this);
                    _control = new RTLSControl(this);
                    _client = new RTLSClient(this);
                    _mainWindow = new MainWindow();
                
                // and its connections here ........
                }
                RTLSControllerApplication::~RTLSControllerApplication()
                {
                    // Delete the objects manually, because we want to control the order
                    delete _mainWindow;
                
                    delete _client;
                    delete _control;
                    delete _cleConnection;
                
                    delete _viewSettings;
                
                    delete _anchorSelectionModel;
                    delete _model;
                
                }
                
                

                RTLSControllerApplication its the main thread ...you can see its called from main function.above

                vk

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 25 Jan 2022, 08:18 last edited by
                  #8

                  Please run your app with the env var QT_FATAL_WARNINGS and take a look at the backtrace to see where the error comes from.
                  Also make a breakpoint in your RTLSClient dtor to see if it is reached.

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

                  V 1 Reply Last reply 7 Feb 2022, 11:57
                  0
                  • Christian EhrlicherC Christian Ehrlicher
                    25 Jan 2022, 08:18

                    Please run your app with the env var QT_FATAL_WARNINGS and take a look at the backtrace to see where the error comes from.
                    Also make a breakpoint in your RTLSClient dtor to see if it is reached.

                    V Offline
                    V Offline
                    Vivek_A
                    wrote on 7 Feb 2022, 11:57 last edited by Vivek_A 2 Jul 2022, 12:14
                    #9

                    I changed the destructor code of RTLSClient to

                    RTLSClient::~RTLSClient()
                    { 
                    workerThread.terminate();
                        if(!workerThread.wait(3000)) //Wait until it actually has terminated (max. 3 sec)
                        {
                            workerThread.terminate(); //Thread didn't exit in time, probably deadlocked, terminate it!
                            workerThread.wait(); //We have to wait again here!
                        }
                    }
                    

                    now app not showing ..QThread: Destroyed while thread is still running but it not responding when opening

                    vk

                    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