From 696b80a59b458220799b6e324ff1af79eb6dc1c7 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 15 Jul 2022 11:19:50 -0700 Subject: [PATCH] Use no-destructor idiom for type_caster_odr_guard_registry, as suggested by @laramiel --- include/pybind11/detail/type_caster_odr_guard.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index 9454eba62..0990942bb 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -28,9 +28,12 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) -inline std::unordered_map &type_caster_odr_guard_registry() { - static std::unordered_map reg; - return reg; +using type_caster_odr_guard_registry_type = std::unordered_map; + +inline type_caster_odr_guard_registry_type &type_caster_odr_guard_registry() { + // Using the no-destructor idiom (maximizes safety). + static auto reg = new type_caster_odr_guard_registry_type(); + return *reg; } inline unsigned &type_caster_odr_violation_detected_counter() {