From 7a91aed7fea7952044ce6df1269f88900f99e013 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 12 Oct 2025 10:50:25 -0700 Subject: [PATCH] [skip ci] Pin-pointing `decref in C++ dtor` Based on manual review of: $ cd include/pybind11/ $ git grep -e Py_DECREF -e Py_XDECREF | cut -d: -f1 | uniq conduit/pybind11_conduit_v1.h detail/class.h detail/cpp_conduit.h detail/type_caster_base.h gil_safe_call_once.h numpy.h pybind11.h pytypes.h stl.h stl/filesystem.h trampoline_self_life_support.h --- include/pybind11/detail/type_caster_base.h | 6 +++--- include/pybind11/trampoline_self_life_support.h | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a2512a5527..073909b10b 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -77,7 +77,7 @@ class loader_life_support { } frame = parent; for (auto *item : keep_alive) { - Py_DECREF(item); + Py_DECREF(item); // decref in C++ dtor: always safe? } } @@ -727,7 +727,7 @@ struct shared_ptr_parent_life_support { // NOLINTNEXTLINE(readability-make-member-function-const) void operator()(void *) { gil_scoped_acquire gil; - Py_DECREF(parent); + Py_DECREF(parent); // decref in C++ dtor: NEEDS can_decref_now } }; @@ -741,7 +741,7 @@ struct shared_ptr_trampoline_self_life_support { // NOLINTNEXTLINE(readability-make-member-function-const) void operator()(void *) { gil_scoped_acquire gil; - Py_DECREF(self); + Py_DECREF(self); // decref in C++ dtor: NEEDS can_decref_now } }; diff --git a/include/pybind11/trampoline_self_life_support.h b/include/pybind11/trampoline_self_life_support.h index cbfec7f974..7715aa271f 100644 --- a/include/pybind11/trampoline_self_life_support.h +++ b/include/pybind11/trampoline_self_life_support.h @@ -42,7 +42,9 @@ struct trampoline_self_life_support { v_h.value_ptr() = nullptr; v_h.holder().release_disowned(); detail::deregister_instance(v_h.inst, value_void_ptr, v_h.type); - Py_DECREF((PyObject *) v_h.inst); // Must be after deregister. + Py_DECREF( + (PyObject *) + v_h.inst); // Must be after deregister. // decref in C++ dtor: always safe? PyGILState_Release(threadstate); } }