Skip to content

Support transfer of ownership of Python subclasses of pybind11 C++ classes with shared_ptr<>, avoid slicing #1145

Closed
@EricCousineau-TRI

Description

@EricCousineau-TRI

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    holdersIssues and PRs regarding holders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions