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. Destroyed while thread is still running
Qt 6.11 is out! See what's new in the release blog

Destroyed while thread is still running

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.2k 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.
  • N Offline
    N Offline
    neda
    wrote on last edited by neda
    #1

    Hi
    When I close my application, it still exists in the task manager.
    I try to kill all threads to solve this problem.

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
        QQuickView engine;
    
        QApplication::setOrganizationName("myapp");
        QApplication::setOrganizationDomain("myapp");
            
        PCAPNetwork myPort;    
       
        /* Thread */
        QThread::currentThread()->setObjectName("MainThread");
        QThread * thread = new QThread();
        thread->setObjectName("PortThread");
        myPort.moveToThread(thread);
        thread->start();
    
        QObject::connect(thread, SIGNAL(started()), &myPort, SLOT(openPort()));
        QObject::connect(thread, SIGNAL(finished()), thread, SLOT(quit()));
        QObject::connect(thread, SIGNAL(finished()), thread, SLOT(terminate()));
        QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        QObject::connect(thread, &QThread::finished, qApp, &QObject::deleteLater);
        QObject::connect(thread, &QThread::finished, &app, &QObject::deleteLater);
    
        QObject::connect(&app, &QApplication::aboutToQuit, thread, &QThread::quit);
        QObject::connect(&app, &QApplication::aboutToQuit, thread, &QThread::terminate);
               
    
        QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(quit()));
        QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(terminate()));
        QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(deleteLater()));
    
        /* End Thread */
        /* End PCAPNetwork */
    
        engine.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
        
        
        int result = QApplication::exec();
    
        thread->quit();
        thread->terminate();
        thread->wait();
        return result;
    }
    
    

    I have written a lot of codes to try, but I still have this error.

    QThread: Destroyed while thread is still running
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • N neda

      Hi
      When I close my application, it still exists in the task manager.
      I try to kill all threads to solve this problem.

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QApplication app(argc, argv);
          QQuickView engine;
      
          QApplication::setOrganizationName("myapp");
          QApplication::setOrganizationDomain("myapp");
              
          PCAPNetwork myPort;    
         
          /* Thread */
          QThread::currentThread()->setObjectName("MainThread");
          QThread * thread = new QThread();
          thread->setObjectName("PortThread");
          myPort.moveToThread(thread);
          thread->start();
      
          QObject::connect(thread, SIGNAL(started()), &myPort, SLOT(openPort()));
          QObject::connect(thread, SIGNAL(finished()), thread, SLOT(quit()));
          QObject::connect(thread, SIGNAL(finished()), thread, SLOT(terminate()));
          QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          QObject::connect(thread, &QThread::finished, qApp, &QObject::deleteLater);
          QObject::connect(thread, &QThread::finished, &app, &QObject::deleteLater);
      
          QObject::connect(&app, &QApplication::aboutToQuit, thread, &QThread::quit);
          QObject::connect(&app, &QApplication::aboutToQuit, thread, &QThread::terminate);
                 
      
          QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(quit()));
          QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(terminate()));
          QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(deleteLater()));
      
          /* End Thread */
          /* End PCAPNetwork */
      
          engine.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
          
          
          int result = QApplication::exec();
      
          thread->quit();
          thread->terminate();
          thread->wait();
          return result;
      }
      
      

      I have written a lot of codes to try, but I still have this error.

      QThread: Destroyed while thread is still running
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @neda said in Destroyed while thread is still running:

      PCAPNetwork

      You should look where this class blocks the thread event loop so QThread::quit() can't do anything.

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

      N 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @neda said in Destroyed while thread is still running:

        PCAPNetwork

        You should look where this class blocks the thread event loop so QThread::quit() can't do anything.

        N Offline
        N Offline
        neda
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        Thanks
        How can I find where this class blocks the thread?
        This class is very simple and has just three functions (openPort, packet_handler, sendData).

        Christian EhrlicherC 1 Reply Last reply
        0
        • N neda

          @Christian-Ehrlicher

          Thanks
          How can I find where this class blocks the thread?
          This class is very simple and has just three functions (openPort, packet_handler, sendData).

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @neda said in Destroyed while thread is still running:

          How can I find where this class blocks the thread?

          Look at the source maybe? We don't have them...
          When a thread can not quit you're blocking it's execution. Don't do this.

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

          N 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @neda said in Destroyed while thread is still running:

            How can I find where this class blocks the thread?

            Look at the source maybe? We don't have them...
            When a thread can not quit you're blocking it's execution. Don't do this.

            N Offline
            N Offline
            neda
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Thank you
            That's right
            After open the port, I have a loop.

            /* start the capture */
                pcap_loop(adhandle, 0, packet_handler, NULL);
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The documentation to pcap_loop tells you how to stop this function.

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

              N 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                The documentation to pcap_loop tells you how to stop this function.

                N Offline
                N Offline
                neda
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Thank you

                void PCAPNetwork::closePort()
                {
                  qDebug()<<"close port .... ";
                   pcap_breakloop(adhandle);
                   pcap_close(adhandle);
                }
                

                The "closePort" function is never called.

                QObject::connect(qApp, SIGNAL(aboutToQuit()), &myPort, SLOT(closePort()));
                QObject::connect(qApp, SIGNAL(aboutToQuit()), thread, SLOT(deleteLater()));
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  QCoreApplication::aboutToQuit() is called just before QCoreApplication gets destroyed. So how should this work?

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

                  N 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    QCoreApplication::aboutToQuit() is called just before QCoreApplication gets destroyed. So how should this work?

                    N Offline
                    N Offline
                    neda
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    Thanks
                    That's right

                    main.qml
                    onClosing: {
                            myPort.closePort();
                        }
                    

                    But I have the same error.
                    I'm trying to check codes of function "closePort"

                    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