From 86d801e352ded9d1fc5f02a18386c63012bce16d Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Tue, 8 Aug 2023 18:21:55 +0200 Subject: [PATCH] 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 Reviewed-by: Qt CI Bot Reviewed-by: Marc Mutz --- src/corelib/kernel/qapplicationstatic.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/corelib/kernel/qapplicationstatic.h b/src/corelib/kernel/qapplicationstatic.h index f2f09118562..bf5e79b8bf7 100644 --- a/src/corelib/kernel/qapplicationstatic.h +++ b/src/corelib/kernel/qapplicationstatic.h @@ -51,6 +51,8 @@ template 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); }