Skip to content

Commit f743bdf

Browse files
authored
Avoid local_internals destruction (#4192)
* Avoid local_internals destruction It allows to avoid possible static deinitialization fiasco. * Add link to relevant google style guide discussion
1 parent 95d0e71 commit f743bdf

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

include/pybind11/detail/internals.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,13 @@ struct local_internals {
514514

515515
/// Works like `get_internals`, but for things which are locally registered.
516516
inline local_internals &get_local_internals() {
517-
static local_internals locals;
518-
return locals;
517+
// Current static can be created in the interpreter finalization routine. If the later will be
518+
// destroyed in another static variable destructor, creation of this static there will cause
519+
// static deinitialization fiasco. In order to avoid it we avoid destruction of the
520+
// local_internals static. One can read more about the problem and current solution here:
521+
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
522+
static auto *locals = new local_internals();
523+
return *locals;
519524
}
520525

521526
/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its

0 commit comments

Comments
 (0)