diff options
author | Thiago Macieira <[email protected]> | 2025-02-09 11:13:19 -0800 |
---|---|---|
committer | Marc Mutz <[email protected]> | 2025-02-26 06:45:07 +0000 |
commit | d09c6c1046ef008851af9dc698cab49f1e779c9b (patch) | |
tree | e389694e07e4b613074e2c87952bc83534565583 /src/sql/kernel/qsqldatabase.cpp | |
parent | 8b75f4735b36a77b6426c7dab55190416e6a4af5 (diff) |
Short live QCoreApplication::instanceExists()
This is a thread-safe version of
QCoreApplication::instance() != nullptr
for Qt 6.x, because QCoreApplication::self is not atomic and thus
reading it from outside the main thread could be a data race.
That's not to say it always is: if by construction the code can only run
in the main thread or while QCoreApplication definitely exists, that's
safe. Therefore, this commit focuses on places that are meant to be used
in multi-threaded environment (ruling out most of QtGui and QtWidgets)
or where the code was going to dereference the returned pointer anyway.
Change-Id: I6fc556c5fe5cbe0b5902fffdfb6b3bb345b0ee50
Reviewed-by: Marc Mutz <[email protected]>
Diffstat (limited to 'src/sql/kernel/qsqldatabase.cpp')
-rw-r--r-- | src/sql/kernel/qsqldatabase.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/sql/kernel/qsqldatabase.cpp b/src/sql/kernel/qsqldatabase.cpp index bb7d5a1ea53..70840010555 100644 --- a/src/sql/kernel/qsqldatabase.cpp +++ b/src/sql/kernel/qsqldatabase.cpp @@ -23,12 +23,12 @@ Q_STATIC_LOGGING_CATEGORY(lcSqlDb, "qt.sql.qsqldatabase") using namespace Qt::StringLiterals; #define CHECK_QCOREAPPLICATION \ - if (Q_UNLIKELY(!QCoreApplication::instance())) { \ + if (Q_UNLIKELY(!QCoreApplication::instanceExists())) { \ qCWarning(lcSqlDb, "QSqlDatabase requires a QCoreApplication"); \ return; \ } #define CHECK_QCOREAPPLICATION_RETVAL \ - if (Q_UNLIKELY(!QCoreApplication::instance())) { \ + if (Q_UNLIKELY(!QCoreApplication::instanceExists())) { \ qCWarning(lcSqlDb, "QSqlDatabase requires a QCoreApplication"); \ return {}; \ } @@ -661,7 +661,7 @@ void QSqlDatabasePrivate::init(const QString &type) qCWarning(lcSqlDb, "QSqlDatabase: %ls driver not loaded", qUtf16Printable(type)); qCWarning(lcSqlDb, "QSqlDatabase: available drivers: %ls", qUtf16Printable(QSqlDatabase::drivers().join(u' '))); - if (QCoreApplication::instance() == nullptr) + if (!QCoreApplication::instanceExists()) qCWarning(lcSqlDb, "QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins"); driver = shared_null()->driver; } |