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

Error Instantiating QCoreApplication inside a Thread



  • Hi,

    I need to have a QCoreApplication instance inside a Thread (I need the event loop to work).

    So tried instantiating a QCoreApplication.

    QtConcurrent::run([=]() {
        int argc = 1;
        char *argv[] = {"sync"};
    
        app = new QCoreApplication(argc, argv);
    });
    

    But got the following error :

    libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x9c in tid 15443 (Thread (pooled))
    

    Any suggestions or ideas?


  • Qt Champions 2019

    @ab0027 Why do you want to put QCoreApplication into another thread? Put long lasting operations in other threads instead.
    Also where is app declared?



  • @jsulm Sorry for the delayed response. Was out on weekend.

    Actually I already have a application and realised that I need to do that operation in a thread as it is blocking the UI.

    The preferred solution for my existing application would be to able to wrap my existing codebase in a thread.
    Putting the long lasting operations in other threads would be quite a hassle.

    Isn't there any possible way to have a QCoreApplication Instance in a seperate thread?
    Also I dont understand why I am getting the libc error? Any ideas?

    P.S. : app is declared in the header file of the class. I just posted a code snippet and not the entire thing with class definition and all.


  • Qt Champions 2019

    @ab0027 You forgot to call exec() on your app QCoreApplication instance.
    Did you try to debug to see what exactly happens?



  • @jsulm No. I didn't forget.

    I get the libc error even if I just instantiate the QCoreApplication without calling exec().

    To test things out, I tried with QEventLoop. I get the same libc error even if I just instantiate the QEventLoop object inside the thread.

    QtConcurrent::run([=]() { QEventLoop q; });
    

  • Lifetime Qt Champion

    @ab0027 Q(Core)Application must be the first QObject in your app.

    If you want to run this in another thread, and use Qt functions to manage the thread, you can expect problems.


  • Qt Champions 2017

    @aha_1980 said in Error Instantiating QCoreApplication inside a Thread:

    you can expect problems

    Big problems™

    @ab0027
    Rethink your design. You can't have anything QObject derived before QCoreApplication, as @aha_1980 noted. This means you're restricted to one of two possibilities:

    1. std::thread
    2. OS-specific threading

    On top of that, if memory serves, a known limitation on macOS is that you can't run the (main) event loop in a thread that's outside main. So you're begging for trouble here.



  • So I do need to rethink my design here it seems..

    I am also facing another issue with QtAndroid.
    I have a QtService(Java) from which I need to call some native function which need Qt Event loop to function properly.

    When the native method is called from the Java QtService, the main application will not be running.
    So, I loaded the library with System.loadLibrary() from the Java QtService and also added the manifest metadatas to load the qt libraries.

    If I call the native method which just has a qDebug(), I am getting the log.
    But if I instantiate a QCoreApplication / QAndroidService object, I get the same libc error :

    libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 14264 (SyncThread)
    

    Any ideas??


  • Qt Champions 2017

    @ab0027 said in Error Instantiating QCoreApplication inside a Thread:

    But if I instantiate a QCoreApplication / QAndroidService object, I get the same libc error :

    Get a stack trace. This segfault means there's a dereferncing of a null pointer somewhere somehow. Why and how can't be deduced from the error. If I were to hazard a guess, you've created some QObject globally and/or you have called something Qt before you have QCoreApplication ready to roll ...



  • @kshegunov
    I would love to have a stack trace too.. I get nothing except the libc error from logcat. No stacktrace or any other error.
    And the very first line in the native function is the instantiation of QCoreApplication.

    I tried testing with QEventLoop and found that if I instantiate a QEventLoop Object, there are no errors being reported. Also I was able to start the event loop. But I slots and signals are not working.

    Here is a code snippet :

    QEventLoop* qEventLoop = new QEventLoop();
    
    QTimer* t = new QTimer();
    QObject::connect(t, &QTimer::timeout, [=]() {
      qDebug() << "timeout() called";
      qEventLoop->exit(0);
    });
    t->start(2000);
    
    qEventLoop->exec();
    

    Here the event loop starts but the slot is never called. Am I doing something wring here?


Log in to reply