From 38564297e778f2197ceb1d829741192d44bb6082 Mon Sep 17 00:00:00 2001 From: JiDe Zhang Date: Sat, 16 Jan 2021 04:27:39 +0800 Subject: fix: The QtStartUpFunction function may be called repeatedly Don't call pre routine function in qAddPreRoutine if the qt_call_pre_routines is not called Task-number: QTBUG-90341 Change-Id: I0ee70561dc57b857f8b3b1cf42c9dfe0cf45bd49 Reviewed-by: Alexandru Croitor (cherry picked from commit 22e967c3049608f82abd32a0beb0b4b36ee134bf) --- src/corelib/kernel/qcoreapplication.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 67cc56449a..9c1f2afcb1 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -252,6 +252,7 @@ Q_GLOBAL_STATIC(QStartUpFuncList, preRList) typedef QList QVFuncList; Q_GLOBAL_STATIC(QVFuncList, postRList) static QBasicMutex globalRoutinesMutex; +static bool preRoutinesCalled = false; /*! \internal @@ -265,8 +266,10 @@ void qAddPreRoutine(QtStartUpFunction p) if (!list) return; - if (QCoreApplication::instance()) + if (preRoutinesCalled) { + Q_ASSERT(QCoreApplication::instance()); p(); + } // Due to C++11 parallel dynamic initialization, this can be called // from multiple threads. @@ -294,6 +297,9 @@ void qRemovePostRoutine(QtCleanUpFunction p) static void qt_call_pre_routines() { + // After will be allowed invoke QtStartUpFunction when calling qAddPreRoutine + preRoutinesCalled = true; + if (!preRList.exists()) return; @@ -855,6 +861,8 @@ void QCoreApplicationPrivate::init() */ QCoreApplication::~QCoreApplication() { + preRoutinesCalled = false; + qt_call_post_routines(); self = nullptr; -- cgit v1.2.3