Skip to content

Commit

Permalink
Avoid local_internals destruction (#4192)
Browse files Browse the repository at this point in the history
* Avoid local_internals destruction

It allows to avoid possible static deinitialization fiasco.

* Add link to relevant google style guide discussion
  • Loading branch information
bogdan-lab authored Sep 21, 2022
1 parent 95d0e71 commit f743bdf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@ struct local_internals {

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

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

0 comments on commit f743bdf

Please sign in to comment.