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.3k 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

    @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
                  • J Offline
                    J Offline
                    jamalabo
                    wrote on last edited by
                    #22

                    @JKSH yes it's initialized to nullptr but still does nothing (the request is not sent)

                    JKSHJ 1 Reply Last reply
                    0
                    • J jamalabo

                      @JKSH yes it's initialized to nullptr but still does nothing (the request is not sent)

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

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

                      yes it's initialized to nullptr

                      OK, so it sounds like initializing nullptr stopped the crash.

                      but still does nothing (the request is not sent)

                      Time to start debugging.

                      • What does your QNetworkRequest look like?
                      • Does the QNetworkAccessManager emit any other signals after you called head()? https://doc.qt.io/qt-5/qnetworkaccessmanager.html#signals
                      • Does the QNetworkReply emit any signals after you called head()? (The QNetworkReply object is returned by QNetworkAccessManager::head()) https://doc.qt.io/qt-5/qnetworkreply.html#signals

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

                      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