Q_APPLICATION_STATIC: Assert existence of a QCoreApplication instance

Add asserting verifying that a QCoreApplication exists when
the application static variable is dereferenced. This is a
requirement described in the documentation. Added assertion
makes invalid use much more visible. Without the assertion
only a message about invalid nullptr used with QObject::connect()
is emitted. This message is much less informative and does
not cause tests to fail.

Pick-to: 6.6
Change-Id: Id9d4a34679ca5aca93ee45ca2318d4ccf849887b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Ievgenii Meshcheriakov 2023-08-08 18:21:55 +02:00
parent 3ca6e7e6c7
commit 86d801e352

View File

@ -51,6 +51,8 @@ template <typename QAS> struct ApplicationHolder
if (guard.loadRelaxed() == QtGlobalStatic::Uninitialized) {
QAS::innerFunction(&storage);
const auto *app = QCoreApplication::instance();
Q_ASSERT_X(app, Q_FUNC_INFO,
"The application static was used without a QCoreApplication instance");
QObject::connect(app, &QObject::destroyed, app, reset, Qt::DirectConnection);
guard.storeRelease(QtGlobalStatic::Initialized);
}