MCPcopy Create free account
hub / github.com/pybind/pybind11 / clear_instance

Function clear_instance

include/pybind11/detail/class.h:455–492  ·  view source on GitHub ↗

Clears all internal data from the instance and removes it from registered instances in preparation for deallocation.

Source from the content-addressed store, hash-verified

453/// Clears all internal data from the instance and removes it from registered instances in
454/// preparation for deallocation.
455inline 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.

Callers 1

pybind11_object_deallocFunction · 0.85

Calls 6

values_and_holdersClass · 0.85
deregister_instanceFunction · 0.85
clear_patientsFunction · 0.85
instance_registeredMethod · 0.80
holder_constructedMethod · 0.80
deallocate_layoutMethod · 0.80

Tested by

no test coverage detected