Fix smart_holder def_readwrite for non-smart-holder member types#6003
Open
virtuald wants to merge 2 commits intopybind:masterfrom
Open
Fix smart_holder def_readwrite for non-smart-holder member types#6003virtuald wants to merge 2 commits intopybind:masterfrom
virtuald wants to merge 2 commits intopybind:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Ran into an issue where my extension was failing with pybind11 calling free on an enum. Asked AI to dig into it and it got it wrong several times, but it finally did hit on a really subtle bug (described below).
Honestly, the fix and tests are probably slop -- I'm not sure why it decided to run the test in a subprocess, but the fix looks right and the tests pass. Open to suggestions.
gpt-5.3-high's analysis of the bug
This comes from a subtle interaction of generated bindings + smart_holder:
target types it eventually treats holder memory as if it were unique_ptr.
So pybind is not “randomly” deleting Level; it’s getting put into an invalid ownership state via holder-type mismatch.
Summarizing code locations with excerpts
Absolutely — here are the exact places.
1) def_readwrite routes through property_cpp_function<type, D>::read
2) For smart-holder classes with by-value members, read returns std::shared_ptr
That is exactly the std::shared_ptr(c_sp, &(c_sp.get()->*pm)) aliasing getter.
3) This specialization is selected for by-value non-pointer/non-smart-pointer members
Suggested changelog entry: