From bb8aada627e23a7f7e23dfdb97d443f1b847086a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4=C3=A4tt=C3=A4?= Date: Mon, 20 Mar 2023 08:17:52 +0200 Subject: [PATCH] Fix crash at exit when tracing The crash is caused by the cleanup sending trace messages when the plugin has already been destroyed. Add shutdown callback to the plugin to indicate this has happened. We can't use signals since that also generetes trace event. Pick-to: 6.5 Change-Id: I2e490fc51c2aaa97c240c1496a528a6ff6077bd0 Reviewed-by: Hatem ElKharashy Reviewed-by: Janne Koskinen --- src/corelib/tracing/qctf.cpp | 10 +++------- src/corelib/tracing/qctf_p.h | 1 + src/plugins/tracing/qctflib.cpp | 1 + src/plugins/tracing/qctflib_p.h | 4 ++++ src/plugins/tracing/qctfplugin.cpp | 7 ++++++- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/corelib/tracing/qctf.cpp b/src/corelib/tracing/qctf.cpp index 1dafa582d9..00a196c926 100644 --- a/src/corelib/tracing/qctf.cpp +++ b/src/corelib/tracing/qctf.cpp @@ -15,6 +15,7 @@ QT_BEGIN_NAMESPACE static bool s_initialized = false; static bool s_triedLoading = false; static bool s_prevent_recursion = false; +static bool s_shutdown = false; static QCtfLib* s_plugin = nullptr; #if defined(Q_OS_ANDROID) @@ -59,18 +60,13 @@ static bool loadPlugin(bool &retry) s_plugin = qobject_cast(loader.instance()); if (!s_plugin) return false; - QObject *obj = loader.instance(); - if (obj) { - QObject::connect(obj, &QObject::destroyed, []() { - s_plugin = nullptr; - }); - } + s_plugin->shutdown(&s_shutdown); return true; } static bool initialize() { - if (s_prevent_recursion) + if (s_shutdown || s_prevent_recursion) return false; if (s_initialized || s_triedLoading) return s_initialized; diff --git a/src/corelib/tracing/qctf_p.h b/src/corelib/tracing/qctf_p.h index 0e39b74e9e..b8db869e09 100644 --- a/src/corelib/tracing/qctf_p.h +++ b/src/corelib/tracing/qctf_p.h @@ -232,6 +232,7 @@ public: virtual void doTracepoint(const QCtfTracePointEvent &point, const QByteArray &arr) = 0; virtual bool sessionEnabled() = 0; virtual QCtfTracePointPrivate *initializeTracepoint(const QCtfTracePointEvent &point) = 0; + virtual void shutdown(bool *shutdown) = 0; }; QT_END_NAMESPACE diff --git a/src/plugins/tracing/qctflib.cpp b/src/plugins/tracing/qctflib.cpp index c3fdeed660..bfbe6b2d87 100644 --- a/src/plugins/tracing/qctflib.cpp +++ b/src/plugins/tracing/qctflib.cpp @@ -50,6 +50,7 @@ void QCtfLibImpl::cleanup() { if (s_instance) delete s_instance; + s_instance = nullptr; } QCtfLibImpl::QCtfLibImpl() diff --git a/src/plugins/tracing/qctflib_p.h b/src/plugins/tracing/qctflib_p.h index 081dda1d04..d92730ec57 100644 --- a/src/plugins/tracing/qctflib_p.h +++ b/src/plugins/tracing/qctflib_p.h @@ -78,6 +78,10 @@ public: QCtfTracePointPrivate *initializeTracepoint(const QCtfTracePointEvent &point) override; void registerMetadata(const QCtfTraceMetadata &metadata); int eventId(); + void shutdown(bool *) override + { + + } static QCtfLib *instance(); static void cleanup(); diff --git a/src/plugins/tracing/qctfplugin.cpp b/src/plugins/tracing/qctfplugin.cpp index 8f2245bb28..daa2b3637e 100644 --- a/src/plugins/tracing/qctfplugin.cpp +++ b/src/plugins/tracing/qctfplugin.cpp @@ -22,9 +22,13 @@ public: ~QCtfTracePlugin() { m_cleanup = true; + *m_shutdown = true; QCtfLibImpl::cleanup(); } - + void shutdown(bool *shutdown) override + { + m_shutdown = shutdown; + } bool tracepointEnabled(const QCtfTracePointEvent &point) override { if (m_cleanup) @@ -51,6 +55,7 @@ public: } private: bool m_cleanup = false; + bool *m_shutdown = nullptr; }; #include "qctfplugin.moc"