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() {