Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Destroyed while thread is still running



  • 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
    

  • Lifetime Qt Champion

    @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.



  • @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).


  • Lifetime Qt Champion

    @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.



  • @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);
    

  • Lifetime Qt Champion

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



  • @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()));
    

  • Lifetime Qt Champion

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



  • @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"


Log in to reply