Clears all internal data from the instance and removes it from registered instances in preparation for deallocation.
| 453 | /// Clears all internal data from the instance and removes it from registered instances in |
| 454 | /// preparation for deallocation. |
| 455 | inline void clear_instance(PyObject *self) { |
| 456 | auto *instance = reinterpret_cast<detail::instance *>(self); |
| 457 | |
| 458 | // Deallocate any values/holders, if present: |
| 459 | for (auto &v_h : values_and_holders(instance)) { |
| 460 | if (v_h) { |
| 461 | |
| 462 | // We have to deregister before we call dealloc because, for virtual MI types, we still |
| 463 | // need to be able to get the parent pointers. |
| 464 | if (v_h.instance_registered() |
| 465 | && !deregister_instance(instance, v_h.value_ptr(), v_h.type)) { |
| 466 | pybind11_fail( |
| 467 | "pybind11_object_dealloc(): Tried to deallocate unregistered instance!"); |
| 468 | } |
| 469 | |
| 470 | if (instance->owned || v_h.holder_constructed()) { |
| 471 | v_h.type->dealloc(v_h); |
| 472 | } |
| 473 | } else if (v_h.holder_constructed()) { |
| 474 | v_h.type->dealloc(v_h); // Disowned instance. |
| 475 | } |
| 476 | } |
| 477 | // Deallocate the value/holder layout internals: |
| 478 | instance->deallocate_layout(); |
| 479 | |
| 480 | if (instance->weakrefs) { |
| 481 | PyObject_ClearWeakRefs(self); |
| 482 | } |
| 483 | |
| 484 | PyObject **dict_ptr = _PyObject_GetDictPtr(self); |
| 485 | if (dict_ptr) { |
| 486 | Py_CLEAR(*dict_ptr); |
| 487 | } |
| 488 | |
| 489 | if (instance->has_patients) { |
| 490 | clear_patients(self); |
| 491 | } |
| 492 | } |
| 493 | |
| 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. |
no test coverage detected