[BUG]: pybind11 should not expect there is always a shared_ptr when detecting enable_shared_from_this
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
Problem description
Currently, when pybind11 detects a class inheriting std::enable_shared_from_this, it asserts users always uses std::shared_ptr to hold it. Specifically, it checks std::shared_ptr::element_type at this line:
However, even when a class inherits std::enable_shared_from_this, an object of its does not necessarily be held in a std::shared_ptr. It can be held by std::shared_ptr in just some part of the code, and held by other holders when passing to pybind11. In my case, I warped around std::shared_ptr to build my own holder, which does not have an element_type member type.
A possible fix is to check the type of holder_type first before assuming it is a std::shared_ptr.
Reproducible example code
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at include/pybind11/pybind11.h around line 1784, where enable_shared_from_this handling inspects the holder type. Trace the surrounding holder and shared_ptr detection logic, then verify that custom holders without an element_type are handled without the assertion; add or update regression coverage if the repository's nearby tests expose this path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100