Support transfer of ownership of Python subclasses of pybind11 C++ classes with shared_ptr<>, avoid slicing · Issue #1145 · pybind/pybind11 (original) (raw)

Update (2020-12-22): Simplest reproduction can be seen in #1333


If a Python subclass of a pybind11 C++ class, with holder_type = shared_ptr<T>, has a shared_ptr<> that exists in C++ when its Python ref_count() goes to zero, Python will destroy the Python extension, and leave the C++ Alias type (e.g. PyBase, using PYBIND11_OVERLOAD*) functionality bare (and actually aliased / truncated affected by object slicing).

This could be prevented if a custom __del__, PyTypeObject::tp_del, or PyTypeObject::tp_dealloc is installed such that the instance could be resurrected if holder.use_count() > 1 for the given value-holder, and the ownership is released to C++.
(__del__ is the easiest, as Python explicitly mentions that you can increase the refcount of a destructing object and save it from death.)

This is related to #1132.