From d89fff6c7c6a519fcf50609f98e0a3f56e158e1c Mon Sep 17 00:00:00 2001 From: lixingcong Date: Sat, 17 Aug 2024 22:33:53 +0800 Subject: [PATCH 1/2] Initialize function-local static variables using "T& t = *new T" --- include/spdlog/details/console_globals.h | 4 ++-- include/spdlog/details/registry-inl.h | 2 +- include/spdlog/mdc.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h index 9c552106a..5710fd4d2 100644 --- a/include/spdlog/details/console_globals.h +++ b/include/spdlog/details/console_globals.h @@ -12,7 +12,7 @@ namespace details { struct console_mutex { using mutex_t = std::mutex; static mutex_t &mutex() { - static mutex_t s_mutex; + static mutex_t& s_mutex = *new mutex_t; return s_mutex; } }; @@ -20,7 +20,7 @@ struct console_mutex { struct console_nullmutex { using mutex_t = null_mutex; static mutex_t &mutex() { - static mutex_t s_mutex; + static mutex_t& s_mutex = *new mutex_t; return s_mutex; } }; diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index f447848ee..dc5689288 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -234,7 +234,7 @@ SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *gl } SPDLOG_INLINE registry ®istry::instance() { - static registry s_instance; + static registry& s_instance = *new registry; return s_instance; } diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h index 41f0c1f3f..7024db04c 100644 --- a/include/spdlog/mdc.h +++ b/include/spdlog/mdc.h @@ -38,7 +38,7 @@ class SPDLOG_API mdc { static void clear() { get_context().clear(); } static mdc_map_t &get_context() { - static thread_local mdc_map_t context; + static thread_local mdc_map_t& context = *new mdc_map_t; return context; } }; From 8841b7e2d516bcec7a6a27df62952960277488e2 Mon Sep 17 00:00:00 2001 From: lixingcong Date: Tue, 20 Aug 2024 08:21:38 +0800 Subject: [PATCH 2/2] Restore init thread-local var in mdc.h, not use reference-init --- include/spdlog/mdc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h index 7024db04c..41f0c1f3f 100644 --- a/include/spdlog/mdc.h +++ b/include/spdlog/mdc.h @@ -38,7 +38,7 @@ class SPDLOG_API mdc { static void clear() { get_context().clear(); } static mdc_map_t &get_context() { - static thread_local mdc_map_t& context = *new mdc_map_t; + static thread_local mdc_map_t context; return context; } };