diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 5a175b1341..8450d53589 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -254,13 +254,16 @@ inline void finalize_interpreter() { if (builtins.contains(id) && isinstance(builtins[id])) { internals_ptr_ptr = capsule(builtins[id]); } + + Py_Finalize(); + // Local internals contains data managed by the current interpreter, so we must clear them to // avoid undefined behaviors when initializing another interpreter + // Must be cleared only after Py_Finalize() so atexit and other hooks can still use + // registered_types detail::get_local_internals().registered_types_cpp.clear(); detail::get_local_internals().registered_exception_translators.clear(); - Py_Finalize(); - if (internals_ptr_ptr) { delete *internals_ptr_ptr; *internals_ptr_ptr = nullptr;