@@ -985,6 +985,7 @@ struct copyable_holder_caster<
985
985
986
986
std::weak_ptr<type> potentially_slicing_weak_ptr () {
987
987
if (typeinfo->holder_enum_v == detail::holder_enum_t ::smart_holder) {
988
+ // Reusing shared_ptr code to minimize code complexity.
988
989
shared_ptr_storage
989
990
= sh_load_helper.load_as_shared_ptr (value,
990
991
/* responsible_parent=*/ nullptr ,
@@ -1094,17 +1095,17 @@ PYBIND11_NAMESPACE_END(detail)
1094
1095
// / does NOT keep any derived Python objects alive (see issue #1333).
1095
1096
// /
1096
1097
// / For class_-wrapped types using std::shared_ptr as the holder, the following expressions
1097
- // / produce equivalent results (see tests/test_potentially_slicing_shared_ptr .cpp,py):
1098
+ // / produce equivalent results (see tests/test_potentially_slicing_weak_ptr .cpp,py):
1098
1099
// /
1099
1100
// / - obj.cast<std::shared_ptr<T>>()
1100
- // / - py::potentially_slicing_shared_ptr <T>(obj)
1101
+ // / - py::potentially_slicing_weak_ptr <T>(obj).lock( )
1101
1102
// /
1102
1103
// / For class_-wrapped types with trampolines and using py::smart_holder, obj.cast<>()
1103
1104
// / produces a std::shared_ptr that keeps any derived Python objects alive for its own lifetime,
1104
1105
// / but this is achieved by introducing a std::shared_ptr control block that is independent of
1105
1106
// / the one owned by the py::smart_holder. This can lead to surprising std::weak_ptr behavior
1106
- // / (see issue #5623). An easy solution is to use py::potentially_slicing_shared_ptr <>(obj),
1107
- // / as exercised in tests/test_potentially_slicing_shared_ptr .cpp,py (look for
1107
+ // / (see issue #5623). An easy solution is to use py::potentially_slicing_weak_ptr <>(obj),
1108
+ // / as exercised in tests/test_potentially_slicing_weak_ptr .cpp,py (look for
1108
1109
// / "set_wp_potentially_slicing"). Note, however, that this reintroduces the inheritance
1109
1110
// / slicing issue (see issue #1333). The ideal — but usually more involved — solution is to use
1110
1111
// / a Python weakref to the derived Python object, instead of a C++ base-class std::weak_ptr.
0 commit comments