Skip to content

Commit 5918b2e

Browse files
committed
Adding unit test: test_multiple_registered_instances_for_same_pointee
1 parent 0b30a4a commit 5918b2e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

include/pybind11/detail/smart_holder_type_casters.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ struct smart_holder_type_caster_load {
411411
return to_be_released;
412412
}
413413
if (std::get_deleter<shared_ptr_dec_ref_deleter>(hld.vptr) != nullptr) {
414-
// SMART_HOLDER_WIP: unit test coverage.
414+
// This code is reachable only if there are multiple registered_instances for the
415+
// same pointee.
416+
// SMART_HOLDER_WIP: keep weak_ref?
415417
std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>();
416418
return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr);
417419
}

tests/test_class_sh_trampoline_shared_from_this.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ std::shared_ptr<Sft> make_pure_cpp_sft_shd_ptr(const std::string &history_seed)
9494
return std::make_shared<Sft>(history_seed);
9595
}
9696

97+
std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { return obj; }
98+
9799
} // namespace
98100

99101
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft)
@@ -102,7 +104,9 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash)
102104
TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
103105
py::classh<Sft, SftTrampoline>(m, "Sft")
104106
.def(py::init<std::string>())
105-
.def_readonly("history", &Sft::history);
107+
.def_readonly("history", &Sft::history)
108+
// This leads to multiple entries in registered_instances:
109+
.def(py::init([](const std::shared_ptr<Sft> &existing) { return existing; }));
106110

107111
py::classh<SftSharedPtrStash>(m, "SftSharedPtrStash")
108112
.def(py::init<int>())
@@ -118,4 +122,5 @@ TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
118122
m.def("make_pure_cpp_sft_raw_ptr", make_pure_cpp_sft_raw_ptr);
119123
m.def("make_pure_cpp_sft_unq_ptr", make_pure_cpp_sft_unq_ptr);
120124
m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr);
125+
m.def("pass_through_shd_ptr", pass_through_shd_ptr);
121126
}

tests/test_class_sh_trampoline_shared_from_this.py

+18
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,21 @@ def test_pure_cpp_sft_raw_ptr(make_f):
153153
stash1 = m.SftSharedPtrStash(1)
154154
stash1.AddSharedFromThis(obj)
155155
assert obj.history == "PureCppSft_Stash1AddSharedFromThis"
156+
157+
158+
def test_multiple_registered_instances_for_same_pointee():
159+
obj0 = PySft("PySft")
160+
obj0.attachment_in_dict = "Obj0"
161+
assert m.pass_through_shd_ptr(obj0) is obj0
162+
while True:
163+
obj = m.Sft(obj0)
164+
assert obj is not obj0
165+
obj_pt = m.pass_through_shd_ptr(obj)
166+
# Unpredictable! Because registered_instances is as std::unordered_multimap.
167+
assert obj_pt is obj0 or obj_pt is obj
168+
# Multiple registered_instances for the same pointee can lead to unpredictable results:
169+
if obj_pt is obj0:
170+
assert obj_pt.attachment_in_dict == "Obj0"
171+
else:
172+
assert not hasattr(obj_pt, "attachment_in_dict")
173+
break # Comment out for manual leak checking (use `top` command).

0 commit comments

Comments
 (0)