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

Start new QCoreApplication after shutdown

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4: Low
    • None
    • 5.14.2, 6.0.0, 6.1.0, 5.15.0 RC2, 6.2.0
    • Network: HTTP
    • None
    • Linux
    • 7e5a803c08c38e4fddc4338848768b7cfff4848f, 1304040e5d5af0575cac43aaf1424f72472c7b23, 1304040e5d5af0575cac43aaf1424f72472c7b23, cda98280e (dev), 6c6653719 (6.6)

    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