[Interest] QApplication was not created in the main() thread.

Yauheni Pervenenka yauheni.pervenenka at viber.com
Fri Jun 30 10:10:51 CEST 2023


Hi community,
I was faced with a warning QApplication was not created in the main()
thread. on windows only, it is very rare happen, and I have dig a little
bit a source code and found next situation:
QCoreApplicationPrivate::theMainThread is declared as static variable in
qcoreapplication.cpp
QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread =
Q_BASIC_ATOMIC_INITIALIZER(nullptr);
It is initialized in QThreadData::current method, if use webenginecore, it
is initialized in Q_CONSTRUCTOR_FUNCTION(initialize) in
Qt6WebengineCore.dll, full stack:

>   Qt6Cored.dll!QThreadData::current(bool createIfNecessary) Line 97 C++
>   Qt6Cored.dll!QObject::QObject(QObjectPrivate & dd, QObject * parent)
> Line 928 C++
>   Qt6Cored.dll!QIODevice::QIODevice(QIODevicePrivate & dd, QObject *
> parent) Line 491 C++
>   Qt6Cored.dll!QFileDevice::QFileDevice(QFileDevicePrivate & dd, QObject *
> parent) Line 217 C++
>   Qt6Cored.dll!QFile::QFile(const QString & name) Line 266 C++
>   Qt6Cored.dll!loadRulesFromFile(const QString & filePath) Line 286 C++
>   Qt6Cored.dll!QLoggingRegistry::initializeRules() Line 327 C++
>   Qt6Cored.dll!QLoggingRegistry::QLoggingRegistry() Line 275 C++
>   Qt6Cored.dll!`anonymous
> namespace'::Q_QGS_qtLoggingRegistry::innerFunction(void * pointer) Line 63
> C++
>   Qt6Cored.dll!QtGlobalStatic::Holder<`anonymous
> namespace'::Q_QGS_qtLoggingRegistry>::Holder<`anonymous
> namespace'::Q_QGS_qtLoggingRegistry>() Line 74 C++
>   Qt6Cored.dll!QGlobalStatic<QtGlobalStatic::Holder<`anonymous
> namespace'::Q_QGS_qtLoggingRegistry>>::instance() Line 127 C++
>   Qt6Cored.dll!QGlobalStatic<QtGlobalStatic::Holder<`anonymous
> namespace'::Q_QGS_qtLoggingRegistry>>::operator()() Line 110 C++
>   Qt6Cored.dll!QLoggingRegistry::instance() Line 444 C++
>   Qt6Cored.dll!QLoggingCategory::init(const char * category, QtMsgType
> severityLevel) Line 234 C++
>   Qt6Cored.dll!QLoggingCategory::QLoggingCategory(const char * category,
> QtMsgType enableForLevel) Line 223 C++
>   Qt6Quickd.dll!QSG_LOG_INFO() Line 79 C++
>   Qt6Quickd.dll!`anonymous
> namespace'::QLoggingCategoryMacroHolder<0>::QLoggingCategoryMacroHolder<0>(const
> QLoggingCategory &(*)() catfunc) Line 123 C++
>   Qt6Quickd.dll!QSGRhiSupport::applySettings() Line 237 C++
>   Qt6Quickd.dll!QSGRhiSupport::configure(QSGRendererInterface::GraphicsApi
> api) Line 289 C++
>
> Qt6Quickd.dll!QQuickWindow::setGraphicsApi(QSGRendererInterface::GraphicsApi
> api) Line 3869 C++
>   Qt6WebEngineCored.dll!initialize() Line 248 C++
>   Qt6WebEngineCored.dll!`anonymous
> namespace'::initialize_ctor_class_::initialize_ctor_class_() Line 250 C++
> > Qt6WebEngineCored.dll!`anonymous namespace'::`dynamic initializer for
> 'initialize_ctor_instance_''() Line 250 C++
>

Seems like it is UB because initialization of static variables is
undefined, as I see in Qt6.5 added  Q_CONSTINIT before
QCoreApplicationPrivate::theMainThread declaration, but it will work only
starting from c++20 standard, but is it possible to workaround it?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20230630/c6c2d80f/attachment.htm>


More information about the Interest mailing list