Skip to content

Commit 25c3dc9

Browse files
committed
Debug LOOOK & one-line bug fix:
```diff - auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src); + auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first))); ```
1 parent fcd1d5b commit 25c3dc9

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

include/pybind11/detail/smart_holder_poc.h

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ struct smart_holder {
220220
}
221221

222222
static smart_holder from_raw_ptr_unowned(void *raw_ptr) {
223+
printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout);
223224
smart_holder hld;
224225
hld.vptr.reset(raw_ptr, [](void *) {});
225226
hld.vptr_is_using_noop_deleter = true;
@@ -250,6 +251,7 @@ struct smart_holder {
250251

251252
template <typename T>
252253
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) {
254+
printf("\nLOOOK [%lu] %s:%d\n", (std::size_t) raw_ptr, __FILE__, __LINE__); fflush(stdout);
253255
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
254256
smart_holder hld;
255257
auto gd = make_guarded_builtin_delete<T>(true);
@@ -302,6 +304,7 @@ struct smart_holder {
302304
template <typename T, typename D>
303305
static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
304306
bool void_cast_raw_ptr = false) {
307+
printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout);
305308
smart_holder hld;
306309
hld.rtti_uqp_del = &typeid(D);
307310
hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del);
@@ -333,6 +336,8 @@ struct smart_holder {
333336

334337
template <typename T>
335338
static smart_holder from_shared_ptr(std::shared_ptr<T> shd_ptr) {
339+
printf("\nLOOOK [%lu] %s:%d\n", (std::size_t) shd_ptr.get(), __FILE__, __LINE__); fflush(stdout);
340+
// long *BAD = nullptr; *BAD = 101;
336341
smart_holder hld;
337342
hld.vptr = std::static_pointer_cast<void>(shd_ptr);
338343
hld.vptr_is_external_shared_ptr = true;

include/pybind11/detail/smart_holder_type_casters.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
356356
auto *holder_void_ptr = const_cast<void *>(holder_const_void_ptr);
357357

358358
auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(WrappedType)));
359+
printf("\nLOOOK [%lu] %s:%d\n", (std::size_t) v_h.value_ptr(), __FILE__, __LINE__); fflush(stdout);
360+
// long *BAD = nullptr; *BAD = 101;
359361
if (!v_h.instance_registered()) {
360362
register_instance(inst, v_h.value_ptr(), v_h.type);
361363
v_h.set_instance_registered();
@@ -642,6 +644,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
642644
}
643645

644646
static handle cast(T const *src, return_value_policy policy, handle parent) {
647+
printf("\nLOOOK [%lu] IsBase0Still %s:%d\n", (std::size_t) src, __FILE__, __LINE__); fflush(stdout);
645648
auto st = type_caster_base<T>::src_and_type(src);
646649
return cast_const_raw_ptr( // Originally type_caster_generic::cast.
647650
st.first,
@@ -688,6 +691,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
688691
void *(*copy_constructor)(const void *),
689692
void *(*move_constructor)(const void *),
690693
const void *existing_holder = nullptr) {
694+
printf("\nLOOOK [%lu] IsDerivedAlready %s:%d\n", (std::size_t) _src, __FILE__, __LINE__); fflush(stdout);
691695
if (!tinfo) { // no type info: error will be set already
692696
return handle();
693697
}
@@ -780,6 +784,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
780784
static constexpr auto name = const_name<T>();
781785

782786
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
787+
printf("\nLOOOK [%lu] %s:%d\n", (std::size_t) src.get(), __FILE__, __LINE__); fflush(stdout);
783788
switch (policy) {
784789
case return_value_policy::automatic:
785790
case return_value_policy::automatic_reference:
@@ -821,8 +826,10 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
821826
inst_raw_ptr->owned = true;
822827
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
823828
valueptr = src_raw_void_ptr;
829+
printf("\nLOOOK [%lu] IsBase0 %s:%d\n", (std::size_t) valueptr, __FILE__, __LINE__); fflush(stdout);
830+
printf("\nLOOOK [%lu] IsDerived %s:%d\n", (std::size_t) st.first, __FILE__, __LINE__); fflush(stdout);
824831

825-
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src);
832+
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first)));
826833
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
827834

828835
if (policy == return_value_policy::reference_internal) {

tests/test_mi_debug.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ def test_get_vec_size_raw_ptr_base0():
66
assert m.get_vec_size_raw_ptr_base0(obj) == 5
77

88

9-
def test_get_vec_size_raw_ptr_derived():
9+
def test_get_vec_size_raw_ptr_derived_from_shared():
1010
obj = m.make_derived_as_base0()
1111
print("\nLOOOK", obj)
12+
assert m.get_vec_size_raw_ptr_derived(obj) == 5
13+
14+
def test_get_vec_size_raw_ptr_derived():
1215
obj = m.make_derived_as_base0_raw_ptr()
1316
print("\nLOOOK", obj)
1417
assert m.get_vec_size_raw_ptr_derived(obj) == 5

0 commit comments

Comments
 (0)