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. Error Instantiating QCoreApplication inside a Thread
Forum Updated to NodeBB v4.3 + New Features

Error Instantiating QCoreApplication inside a Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ab0027
    wrote on last edited by ab0027
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • A ab0027

      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?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • A Offline
        A Offline
        ab0027
        wrote on last edited by ab0027
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • A ab0027

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

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          1
          • jsulmJ jsulm

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

            A Offline
            A Offline
            ab0027
            wrote on last edited by ab0027
            #5

            @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; });
            
            aha_1980A kshegunovK 2 Replies Last reply
            0
            • A ab0027

              @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; });
              
              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6

              @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 has to stay free or it will die.

              1 Reply Last reply
              4
              • A ab0027

                @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; });
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3
                • A Offline
                  A Offline
                  ab0027
                  wrote on last edited by ab0027
                  #8

                  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??

                  kshegunovK 1 Reply Last reply
                  0
                  • A ab0027

                    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??

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

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

                    Read and abide by the Qt Code of Conduct

                    A 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

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

                      A Offline
                      A Offline
                      ab0027
                      wrote on last edited by
                      #10

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

                      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