Skip to content

Commit 3e963ce

Browse files
committed
Fix crash in def_readwrite for non-smart-holder properties of smart-holder classes
1 parent 6b7b3d2 commit 3e963ce

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

include/pybind11/pybind11.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,10 +2013,16 @@ struct property_cpp_function_sh_raw_ptr_member {
20132013
// This prevents disowning of the Python object owning the member.
20142014
template <typename T, typename D>
20152015
struct property_cpp_function_sh_member_held_by_value {
2016+
static bool use_smart_holder_member_aliasing() {
2017+
type_info *tinfo = get_type_info(typeid(D), /*throw_if_missing=*/true);
2018+
return tinfo->holder_enum_v == holder_enum_t::smart_holder;
2019+
}
2020+
20162021
template <typename PM, must_be_member_function_pointer<PM> = 0>
20172022
static cpp_function readonly(PM pm, const handle &hdl) {
20182023
type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true);
2019-
if (tinfo->holder_enum_v == holder_enum_t::smart_holder) {
2024+
if (tinfo->holder_enum_v == holder_enum_t::smart_holder
2025+
&& use_smart_holder_member_aliasing()) {
20202026
return cpp_function(
20212027
[pm](handle c_hdl) -> std::shared_ptr<typename std::add_const<D>::type> {
20222028
std::shared_ptr<T> c_sp
@@ -2033,7 +2039,8 @@ struct property_cpp_function_sh_member_held_by_value {
20332039
template <typename PM, must_be_member_function_pointer<PM> = 0>
20342040
static cpp_function read(PM pm, const handle &hdl) {
20352041
type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true);
2036-
if (tinfo->holder_enum_v == holder_enum_t::smart_holder) {
2042+
if (tinfo->holder_enum_v == holder_enum_t::smart_holder
2043+
&& use_smart_holder_member_aliasing()) {
20372044
return cpp_function(
20382045
[pm](handle c_hdl) -> std::shared_ptr<D> {
20392046
std::shared_ptr<T> c_sp

0 commit comments

Comments
 (0)