Skip to content

Commit 50c319d

Browse files
committed
Actually free internals during interpreter shutdown (instead of after)
1 parent 6f8308b commit 50c319d

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

include/pybind11/detail/internals.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,7 @@ struct internals {
308308
internals(internals &&other) = delete;
309309
internals &operator=(const internals &other) = delete;
310310
internals &operator=(internals &&other) = delete;
311-
~internals() = default; // NOTE: destruct/decref python objects in shutdown()
312-
313-
/// shutdown is run during interpreter finalization and can (carefully) interact with Python.
314-
void shutdown() {
311+
~internals() {
315312
Py_XDECREF(static_property_type);
316313
static_property_type = nullptr;
317314

@@ -335,8 +332,7 @@ struct local_internals {
335332
std::forward_list<ExceptionTranslator> registered_exception_translators;
336333
PyTypeObject *function_record_py_type = nullptr;
337334

338-
/// shutdown is run during interpreter finalization and can (carefully) interact with Python.
339-
void shutdown() {
335+
~local_internals() {
340336
Py_XDECREF(function_record_py_type);
341337
function_record_py_type = nullptr;
342338
}
@@ -717,14 +713,12 @@ class internals_pp_manager {
717713

718714
static void internals_shutdown(void *vpp) {
719715
auto *pp = static_cast<std::unique_ptr<InternalsType> *>(vpp);
720-
if (pp && *pp) {
721-
(*pp)->shutdown();
716+
if (pp) {
717+
pp->reset();
722718
}
723-
// Because we cannot guarantee the order of destruction of capsules in the interpreter
724-
// state dict, the internals unique_ptr is not deleted in this capsule destructor.
725-
// The internals (and their unique_ptr owner) cannot be deleted until after the interpreter
726-
// has completely shut down. Final cleanup will be done pybind11::finalize_interpreter if
727-
// pybind11 was embedded, or it will be leaked if this is an extension module.
719+
// Because the unique_ptr is still pointed to by the pp_manager in this and possibly other
720+
// modules, we cannot free the unique_ptr itself until after the interpreter has shut down.
721+
// If this interpreter was not created/owned by pybind11 then this unique_ptr is leaked.
728722
}
729723

730724
std::unique_ptr<InternalsType> *get_or_create_pp_in_state_dict() {

0 commit comments

Comments
 (0)