Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-84234

Start new QCoreApplication after shutdown

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4: Low
    • Resolution: Fixed
    • Affects Version/s: 5.14.2, 5.15.0 RC2
    • Fix Version/s: None
    • Component/s: Network: HTTP
    • Labels:
      None
    • Environment:
      Linux
    • Commits:
      7e5a803c08c38e4fddc4338848768b7cfff4848f, 1304040e5d5af0575cac43aaf1424f72472c7b23, 1304040e5d5af0575cac43aaf1424f72472c7b23

      Description

      We want to provide our current application as a library that can be initialized and shutdown on demand. It seems it works without problem since we smashed all QObjects on shutdown.

      We used the qtHookData <private/qhooks_p.h> to find living QObjects of Qt itself. There are still 2 QThreadPool QObjects after a shutdown so we get a WARNING: QApplication was not created in the main() thread..

       
      If we use the QNetworkAccessManager it will spawn a global QThreadPool. That's fine... but it uses a Q_GLOBAL_STATIC for that.

      https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/thread/qthreadpool.cpp#n48

      Our library will run in a new std::thread as MainThread and after the shutdown all QObjects should be dead. That is not possible with Q_GLOBAL_STATIC. If that could be removed everything should be fine. We can shutdown and the caller could spawn a new instance in same process with a new std::thread again.

      How can Qt avoid the Q_GLOBAL_STATIC for a QObject here? POD would be ok.

      #include <QCoreApplication>
      #include <QNetworkAccessManager>
      #include <QNetworkRequest>
      #include <QNetworkReply>
      #include <iostream>
      #include <thread>
      
      static int count;
      static char** var;
      
      void app()
      {
      	std::cout << "Another start" << std::endl;
      	auto* a = new QCoreApplication (count, var);
      	
      	auto* nm = new QNetworkAccessManager(a);
      	nm->setAutoDeleteReplies(true);
      
      	QNetworkRequest rq(QUrl("https://www.google.de/"));
      	auto* reply = nm->get(rq);
      	QObject::connect(reply, &QNetworkReply::finished, reply, []{
      		QCoreApplication::instance()->exit();
      		std::cout << "Exit called" << std::endl;
      	});
      
      	std::cout << "Execute" << std::endl;
      	a->exec();
      	delete a;
      }
      
      int main(int c, char **v)
      {
      	count = c;
      	var = v;
      
      	std::thread t = std::thread(&app);
      	t.join();
      
      	t = std::thread(&app);
      	t.join();
      
      	return 0;
      }
      

       

      Another start
      Execute
      Exit called
      Another start
      WARNING: QApplication was not created in the main() thread.
      Execute
      Exit called

        Attachments

        For Gerrit Dashboard: QTBUG-84234
        # Subject Branch Project Status CR V

          Activity

            People

            Assignee:
            misery André Klitzing
            Reporter:
            misery André Klitzing
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: