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. SIGSEGV after invoking QML method from C++ thread

SIGSEGV after invoking QML method from C++ thread

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 661 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.
  • M Offline
    M Offline
    MatrixSan
    wrote on last edited by MatrixSan
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      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().

      1 Reply Last reply
      3
      • M Offline
        M Offline
        MatrixSan
        wrote on last edited by
        #3

        Thank you! Now all works well.

        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