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. App crashes when using QCoreApplication in library
Forum Updated to NodeBB v4.3 + New Features

App crashes when using QCoreApplication in library

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 6.2k Views 4 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.
  • J jamalabo

    I have a GUI Application and has a shared library which is the functionality lib, that lib is based on Qt so it uses slots & signals. when calling functions from the lib the GUI throws error: QEventLoop: Cannot be used without QApplication (after connect()) so I tried to create QCoreApplication in the lib and use it, but the GUI crashes after calling the function.

    In the GUI:

        MY_LIB::Manager manager;
    
        connect(&content, &MY_LIB::Manager::processStarted, this, &MainWindow::processStarted);
    
        manager.start();
    

    in the Lib:

    MY_LIB::Manager::Manager() {
        int n = 0;
        this->app = new QCoreApplication(n, nullptr);
    }
    
    
    void MY_LIB::Manager::start() {
     
    // work that is based on events (slots & signals)
     
    }
    
    
    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by Pl45m4
    #2

    @jamalabo

    You need to create your QCoreApplication before you create any other QObject.

    If your Manager is a QObject or makes use of them and uses signals/slots, you can't create your Manager instance first and your QCoreApplication inside its constructor.. This will never work.


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    J 1 Reply Last reply
    2
    • J jamalabo

      I have a GUI Application and has a shared library which is the functionality lib, that lib is based on Qt so it uses slots & signals. when calling functions from the lib the GUI throws error: QEventLoop: Cannot be used without QApplication (after connect()) so I tried to create QCoreApplication in the lib and use it, but the GUI crashes after calling the function.

      In the GUI:

          MY_LIB::Manager manager;
      
          connect(&content, &MY_LIB::Manager::processStarted, this, &MainWindow::processStarted);
      
          manager.start();
      

      in the Lib:

      MY_LIB::Manager::Manager() {
          int n = 0;
          this->app = new QCoreApplication(n, nullptr);
      }
      
      
      void MY_LIB::Manager::start() {
       
      // work that is based on events (slots & signals)
       
      }
      
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #3

      And also, why are you faking the arguments to the QCoreApplication?
      It depends on them to do some stuff with paths and such.
      Pass them along from main()!

      And also (2):
      Why do you create the application object in a library?

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @jamalabo

        You need to create your QCoreApplication before you create any other QObject.

        If your Manager is a QObject or makes use of them and uses signals/slots, you can't create your Manager instance first and your QCoreApplication inside its constructor.. This will never work.

        J Offline
        J Offline
        jamalabo
        wrote on last edited by
        #4

        @Pl45m4 so I'm supposed to give the GUI control of QCoreApplication? or make a parent class that is not QObject in lib that is in control of QCoreApplication

        1 Reply Last reply
        0
        • J jamalabo

          I have a GUI Application and has a shared library which is the functionality lib, that lib is based on Qt so it uses slots & signals. when calling functions from the lib the GUI throws error: QEventLoop: Cannot be used without QApplication (after connect()) so I tried to create QCoreApplication in the lib and use it, but the GUI crashes after calling the function.

          In the GUI:

              MY_LIB::Manager manager;
          
              connect(&content, &MY_LIB::Manager::processStarted, this, &MainWindow::processStarted);
          
              manager.start();
          

          in the Lib:

          MY_LIB::Manager::Manager() {
              int n = 0;
              this->app = new QCoreApplication(n, nullptr);
          }
          
          
          void MY_LIB::Manager::start() {
           
          // work that is based on events (slots & signals)
           
          }
          
          
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          @jamalabo said in App crashes when using QCoreApplication in library:

          this->app = new QCoreApplication(n, nullptr);

          From https://doc.qt.io/qt-6/qcoreapplication.html#QCoreApplication

          Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          J 1 Reply Last reply
          1
          • kshegunovK kshegunov

            And also, why are you faking the arguments to the QCoreApplication?
            It depends on them to do some stuff with paths and such.
            Pass them along from main()!

            And also (2):
            Why do you create the application object in a library?

            J Offline
            J Offline
            jamalabo
            wrote on last edited by
            #6

            @kshegunov I don't understand the second segment, what do you mean application object, if you mean GUI objects then no there is no GUI objects created in the library,
            the library works without any help of the application

            1 Reply Last reply
            0
            • VRoninV VRonin

              @jamalabo said in App crashes when using QCoreApplication in library:

              this->app = new QCoreApplication(n, nullptr);

              From https://doc.qt.io/qt-6/qcoreapplication.html#QCoreApplication

              Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

              J Offline
              J Offline
              jamalabo
              wrote on last edited by
              #7

              @VRonin I have tried to pass the arguments from the QUI main and it still crashed.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jamalabo
                wrote on last edited by
                #8

                I have tried this:
                in GUI

                    MY_LIB::DownloadManager::create_application(argc, argv);
                
                    MY_LIB::DownloadableContent* content = MY_LIB::DownloadManager::create_content(url);
                
                    connect(content, &MY_LIB::DownloadableContent::headersLoaded, this, &MainWindow::headersLoaded);
                
                    content->loadHeaders();
                

                in LIB

                QNetworkAccessManager MY_LIB::DownloadManager::get_network_manager() {
                    return QNetworkAccessManager(get_application());
                }
                
                void MY_LIB::DownloadableContent::loadHeaders() {
                    QNetworkAccessManager manager = DownloadManager::get_network_manager();
                    connect(&manager, &QNetworkAccessManager::finished, this, &DownloadableContent::http_headers_loaded);
                    manager.head(this->gen_request());
                }
                
                void MY_LIB::DownloadableContent::http_headers_loaded(QNetworkReply *reply) {
                // process headers
                    emit headersLoaded();
                }
                
                

                I'm new to c++ & qt so if there is any "bad practice" I'm happy to hear suggestions.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi,

                  Is your GUI application a Qt application as well ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Is your GUI application a Qt application as well ?

                    J Offline
                    J Offline
                    jamalabo
                    wrote on last edited by
                    #10

                    @SGaist yes

                    kshegunovK 1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      So it is likely:

                      • You have a static instance of your DownloadableContent
                      • You try to use it before creating your QApplication object

                      If the first one: stop doing that.
                      If the second, as mentioned clearly in the documentation, QxxxApplication must be instanciated first and before any other QObject.

                      From the looks of it, your DownloadableManager class is either not properly implemented or used but in any case, it's not there that you should create the QApplication instance.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • J jamalabo

                        @SGaist yes

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

                        Look, all of this is going the same way, so I'll spill it:

                        Just structure your application, as one'd normally do (like shown in a Qt example) and the problem is going to go away on its own. You don't appear to really need to wrap around the application object - QCoreApplication instance, to answer your previous question - and you really don't appear to need anything exotic here. So, you'd be doing yourself a service to stick to the beaten path.

                        int main(int argc, char ** argv)
                        {
                             QCoreApplication app(argc, argv);
                             // ...
                             // Stuff that the library provides/needs/executes
                             // You can pass the application object to it here, or get it inside the library directly with qApp, if you need to.
                             return QCoreApplication::exec();
                        }

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        1
                        • J Offline
                          J Offline
                          jamalabo
                          wrote on last edited by
                          #13

                          @kshegunov I don't understand where should I put the QCoreApplication? if the GUI is running on QApplication

                          In GUI

                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              MainWindow w;
                              w.set_arguments(argc, argv);
                              w.show();
                              return a.exec();
                          }
                          

                          question: should I use the same QxxxApplication for the GUI and the LIB? and if so I get the error QEventLoop: Cannot be used without QApplication and If I try to pass the app/MainWindow as the parent it's throw the error QObject: Cannot create children for a parent that is in a different thread

                          kshegunovK JKSHJ 2 Replies Last reply
                          0
                          • J jamalabo

                            @kshegunov I don't understand where should I put the QCoreApplication? if the GUI is running on QApplication

                            In GUI

                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                MainWindow w;
                                w.set_arguments(argc, argv);
                                w.show();
                                return a.exec();
                            }
                            

                            question: should I use the same QxxxApplication for the GUI and the LIB? and if so I get the error QEventLoop: Cannot be used without QApplication and If I try to pass the app/MainWindow as the parent it's throw the error QObject: Cannot create children for a parent that is in a different thread

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

                            No, there's only one QApplication object, the one in main()! Also I really see no reason for you getting the thread errors, are you using threads?

                            Read and abide by the Qt Code of Conduct

                            J 1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              No, there's only one QApplication object, the one in main()! Also I really see no reason for you getting the thread errors, are you using threads?

                              J Offline
                              J Offline
                              jamalabo
                              wrote on last edited by
                              #15

                              @kshegunov no but I'm calling the code on a slot in MainWindow (on button click), (I don't know if this runs on different thread than MainWindow)

                              kshegunovK 1 Reply Last reply
                              0
                              • J jamalabo

                                @kshegunov no but I'm calling the code on a slot in MainWindow (on button click), (I don't know if this runs on different thread than MainWindow)

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

                                @jamalabo said in App crashes when using QCoreApplication in library:

                                I don't know if this runs on different thread than MainWindow

                                It doesn't, then I imagine you have static QObjects that are initialized before they're allowed to, a.k.a. "the QApplication object must always be the first one created and the last destroyed". So, as said, create your application object, then pass control to your library to do whatever it does, and when that's done continue on to start the event loop with exec().

                                Your code looks more or less okay, assuming you've removed all QApplication creation from the MainWindow and/or related classes.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                1
                                • J Offline
                                  J Offline
                                  jamalabo
                                  wrote on last edited by jamalabo
                                  #17

                                  @kshegunov it seems like it's working but not properly

                                  void MY_LIB::DownloadableContent::loadHeaders() {
                                      printf("load-headers\n");
                                      QNetworkAccessManager manager;
                                      printf("manager\n");
                                      connect(&manager, &QNetworkAccessManager::finished, this, &DownloadableContent::http_headers_loaded);
                                      manager.head(this->gen_request());
                                  }
                                  
                                  void MY_LIB::DownloadableContent::http_headers_loaded(QNetworkReply *reply) {
                                      printf("headers-loaded");
                                      emit headersLoaded();
                                  }
                                  

                                  after running this code I only get the output

                                  load-headers
                                  manager
                                  

                                  it seems like slots are not called and it's an event loop issue.
                                  should I pass the QParent (app) param to QNetworkAcessManager?

                                  kshegunovK 1 Reply Last reply
                                  0
                                  • J jamalabo

                                    @kshegunov I don't understand where should I put the QCoreApplication? if the GUI is running on QApplication

                                    In GUI

                                    int main(int argc, char *argv[])
                                    {
                                        QApplication a(argc, argv);
                                        MainWindow w;
                                        w.set_arguments(argc, argv);
                                        w.show();
                                        return a.exec();
                                    }
                                    

                                    question: should I use the same QxxxApplication for the GUI and the LIB? and if so I get the error QEventLoop: Cannot be used without QApplication and If I try to pass the app/MainWindow as the parent it's throw the error QObject: Cannot create children for a parent that is in a different thread

                                    JKSHJ Offline
                                    JKSHJ Offline
                                    JKSH
                                    Moderators
                                    wrote on last edited by JKSH
                                    #18

                                    @jamalabo said in App crashes when using QCoreApplication in library:

                                    I don't understand where should I put the QCoreApplication? if the GUI is running on QApplication

                                    • QApplication inherits QCoreApplication.
                                    • You can only have one instance of Q___Application in your program.

                                    So, you cannot create both a QApplication and a QCoreApplication in the same program.

                                    question: should I use the same QxxxApplication for the GUI and the LIB?

                                    Yes. Definitely. They must share the same QxxxApplication instance. (And since your code uses widgets, then you must use QApplication)

                                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                    1 Reply Last reply
                                    0
                                    • J jamalabo

                                      @kshegunov it seems like it's working but not properly

                                      void MY_LIB::DownloadableContent::loadHeaders() {
                                          printf("load-headers\n");
                                          QNetworkAccessManager manager;
                                          printf("manager\n");
                                          connect(&manager, &QNetworkAccessManager::finished, this, &DownloadableContent::http_headers_loaded);
                                          manager.head(this->gen_request());
                                      }
                                      
                                      void MY_LIB::DownloadableContent::http_headers_loaded(QNetworkReply *reply) {
                                          printf("headers-loaded");
                                          emit headersLoaded();
                                      }
                                      

                                      after running this code I only get the output

                                      load-headers
                                      manager
                                      

                                      it seems like slots are not called and it's an event loop issue.
                                      should I pass the QParent (app) param to QNetworkAcessManager?

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

                                      @jamalabo said in App crashes when using QCoreApplication in library:

                                      it seems like slots are not called and it's an event loop issue.

                                      Well, that doesn't surprise me, your QNetworkAccessManager object dies as you leave the function. So either you make it a member to your DownloadableContent class, or create it on the heap (parented properly). And if you decide on the latter, please don't do it every time you pass through that method, just the first time.

                                      should I pass the QParent (app) param to QNetworkAcessManager?

                                      Not necessarily, although usually you give parents to QObject instances. Since DownloadableContent already derives from QObject, what's wrong with giving the QNetworkAcessManager object a parent of the object which "owns"/"contains" it (a.k.a. this in this case)?

                                      Read and abide by the Qt Code of Conduct

                                      1 Reply Last reply
                                      1
                                      • J Offline
                                        J Offline
                                        jamalabo
                                        wrote on last edited by
                                        #20

                                        @kshegunov
                                        now it crashes.

                                        QNetworkAccessManager *NETWORK_COMBINER_LIB::DownloadableContent::get_manager() {
                                            return (!this->manager ? (this->manager = new QNetworkAccessManager()) : this->manager);
                                        }
                                        
                                        void NETWORK_COMBINER_LIB::DownloadableContent::loadHeaders(QObject *parent) {
                                            printf("load-headers\n");
                                            QNetworkAccessManager *pManager = this->get_manager();
                                            printf("pManager\n");
                                            connect(pManager, &QNetworkAccessManager::finished, this, &DownloadableContent::http_headers_loaded);
                                            pManager->head(this->gen_request());
                                        }
                                        
                                        JKSHJ 1 Reply Last reply
                                        0
                                        • J jamalabo

                                          @kshegunov
                                          now it crashes.

                                          QNetworkAccessManager *NETWORK_COMBINER_LIB::DownloadableContent::get_manager() {
                                              return (!this->manager ? (this->manager = new QNetworkAccessManager()) : this->manager);
                                          }
                                          
                                          void NETWORK_COMBINER_LIB::DownloadableContent::loadHeaders(QObject *parent) {
                                              printf("load-headers\n");
                                              QNetworkAccessManager *pManager = this->get_manager();
                                              printf("pManager\n");
                                              connect(pManager, &QNetworkAccessManager::finished, this, &DownloadableContent::http_headers_loaded);
                                              pManager->head(this->gen_request());
                                          }
                                          
                                          JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by JKSH
                                          #21

                                          @jamalabo said in App crashes when using QCoreApplication in library:

                                          now it crashes.

                                          Look at your debugger's stack trace to see why it crashed.

                                          !this->manager ?
                                          

                                          Did you initialize the pointer to nullptr?

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved