| 494 | /// Instance destructor function for all pybind11 types. It calls `type_info.dealloc` |
| 495 | /// to destroy the C++ object itself, while the rest is Python bookkeeping. |
| 496 | extern "C" inline void pybind11_object_dealloc(PyObject *self) { |
| 497 | auto *type = Py_TYPE(self); |
| 498 | |
| 499 | // If this is a GC tracked object, untrack it first |
| 500 | // Note that the track call is implicitly done by the |
| 501 | // default tp_alloc, which we never override. |
| 502 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) != 0) { |
| 503 | PyObject_GC_UnTrack(self); |
| 504 | } |
| 505 | |
| 506 | #if PY_VERSION_HEX >= 0x030D0000 |
| 507 | // PyObject_ClearManagedDict() is available from Python 3.13+. It must be |
| 508 | // called before tp_free() because on Python 3.14+ tp_free no longer |
| 509 | // implicitly clears the managed dict, which would abandon the refcounts of |
| 510 | // objects stored in __dict__ of py::dynamic_attr() types, causing permanent |
| 511 | // memory leaks. |
| 512 | if (PyType_HasFeature(type, Py_TPFLAGS_MANAGED_DICT)) { |
| 513 | PyObject_ClearManagedDict(self); |
| 514 | } |
| 515 | #endif |
| 516 | |
| 517 | clear_instance(self); |
| 518 | |
| 519 | type->tp_free(self); |
| 520 | |
| 521 | // This was not needed before Python 3.8 (Python issue 35810) |
| 522 | // https://github.com/pybind/pybind11/issues/1946 |
| 523 | Py_DECREF(type); |
| 524 | } |
| 525 | |
| 526 | PYBIND11_WARNING_PUSH |
| 527 | PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls") |
nothing calls this directly
no test coverage detected