SIGSEGV after invoking QML method from C++ thread



  • Hello! I'm trying to write GUI for some console applications and I'm stuck on strange problem...

        QQmlApplicationEngine engine(QUrl::fromLocalFile("test.qml"));
     
        Foo fooObject(&engine);
     
        QThread thread;
        fooObject.moveToThread(&thread);
     
        QObject::connect(&thread, SIGNAL(started()), &fooObject, SLOT(start_slot()));
     
        thread.start();
    

    In Foo header file:

    QProcess* testProcess;
    

    Slot, invoked after thread started:

        void Foo::start_slot()
        {
            testProcess = new QProcess;
            connect(testProcess, SIGNAL(finished(int)), this, SLOT(finish_slot()));
            testProcess->start("uname -a"); //just for example
        }
    

    After completion this slot SIGSEGV occurs:

        void Foo::finish_slot()
        {
            delete (testProcess); 
            QObject* moduleObject = engineObject->rootObjects().first()->findChild<QObject*>("testModule");
            QMetaObject::invokeMethod(moduleObject, "test");
        }
    

    But this works fine...

        void Foo::finish_slot()
        {
            QObject* moduleObject = engineObject->rootObjects().first()->findChild<QObject*>("testModule");
            QMetaObject::invokeMethod(moduleObject, "test");
            delete (testProcess);
        }
    

    Is it bug or my mistake? Thanks!
    I'm using Qt 5.7



  • Hi! It's your fault: testProcess' finished function calls the finish_slot function. After the latter has completed it returns to its caller (the finished function) but before that happens you delete the function's object (testProcess). So you're working with a deleted object and it's pure luck that it works in one case. Instead of calling delete testProcess use testProcess.deleteLater().



  • Thank you! Now all works well.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.