Why is it allowed to create multiple QCoreApplication objects?

Take a look at the following code:

#include <QDebug>
#include <QCoreApplication>

int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);
  qDebug() << QCoreApplication::instance(); // prints QCoreApplication(0x7ffd39656150)
  QCoreApplication app2(argc, argv);
  qDebug() << QCoreApplication::instance(); // prints QCoreApplication(0x7ffd39656160)

  return 0;
}

It looks like it's possible to create multiple QCoreApplication objects, but this should be a singleton. What happens to the first QCoreApplication object created? Is it destroyed or will there be two event loops when exec is called for two objects?

+4
source share
2 answers

It's illegal. You are probably working with the Qt release with disabled versions.

https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qcoreapplication.cpp.html#742 makes it clear that you cannot have two QCoreApplication at the same time:

void QCoreApplicationPrivate::init()
{
    Q_Q(QCoreApplication);

    initLocale();

    Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object");
    QCoreApplication::self = q;

    ....
+6
source

. , . , , , - , - , .

0

Source: https://habr.com/ru/post/1651122/


All Articles