From c98c2c08a5f926b67c40b6363274d23066bfe341 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 27 May 2022 09:45:38 +0200 Subject: QQmlDebug: reliably print the debugger warning When an executable contains some TUs that pass printWarning and some that don't, the warning could have been lost if static initialization first initialized one that had printWarning=false and only then moved to initializing one that had printWarning=true. Typically, a DLL might pass false here, in which case the user application, initialized later, wouldn't have a say in whether the warning was printed. Use an atomic_flag to independently track whether the warning was printed, so that we reliably print the warning when at least one TU in the final executable requested it. [ChangeLog][QtQml][QQmlDebug] The warning about enabled debuggers is now printed when at least one translation unit in the final executable requests it, independent of the order in which translation units are linked/initialized. Pick-to: 6.3 6.2 5.15 Change-Id: I10af0a46ecb82a8b1a1373eb9332d913c03b20f3 Reviewed-by: Ulf Hermann --- src/qml/debugger/qqmldebug.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/qml/debugger') diff --git a/src/qml/debugger/qqmldebug.cpp b/src/qml/debugger/qqmldebug.cpp index 58b8ea2c4f..5619a5fe3e 100644 --- a/src/qml/debugger/qqmldebug.cpp +++ b/src/qml/debugger/qqmldebug.cpp @@ -44,15 +44,24 @@ #include #include +#include #include QT_REQUIRE_CONFIG(qml_debug); QT_BEGIN_NAMESPACE +#if __cplusplus >= 202002L +# define Q_ATOMIC_FLAG_INIT {} +#else +# define Q_ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT // deprecated in C++20 +#endif + +Q_CONSTINIT static std::atomic_flag s_printedWarning = Q_ATOMIC_FLAG_INIT; + QQmlDebuggingEnabler::QQmlDebuggingEnabler(bool printWarning) { - if (!QQmlEnginePrivate::qml_debugging_enabled && printWarning) + if (printWarning && !s_printedWarning.test_and_set(std::memory_order_relaxed)) fprintf(stderr, "QML debugging is enabled. Only use this in a safe environment.\n"); QQmlEnginePrivate::qml_debugging_enabled = true; } -- cgit v1.2.3