Skip to content

Commit a3ef2f3

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 5c56460 commit a3ef2f3

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

include/pybind11/detail/smart_holder_poc.h

Lines changed: 5 additions & 0 deletions
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

Lines changed: 8 additions & 1 deletion
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();
@@ -650,6 +652,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
650652
}
651653

652654
static handle cast(T const *src, return_value_policy policy, handle parent) {
655+
printf("\nLOOOK [%lu] IsBase0Still %s:%d\n", (std::size_t) src, __FILE__, __LINE__); fflush(stdout);
653656
auto st = type_caster_base<T>::src_and_type(src);
654657
if (policy == return_value_policy::_clif_automatic) {
655658
policy = return_value_policy::copy;
@@ -702,6 +705,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
702705
void *(*copy_constructor)(const void *),
703706
void *(*move_constructor)(const void *),
704707
const void *existing_holder = nullptr) {
708+
printf("\nLOOOK [%lu] IsDerivedAlready %s:%d\n", (std::size_t) _src, __FILE__, __LINE__); fflush(stdout);
705709
if (!tinfo) { // no type info: error will be set already
706710
return handle();
707711
}
@@ -794,6 +798,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
794798
static constexpr auto name = const_name<T>();
795799

796800
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
801+
printf("\nLOOOK [%lu] %s:%d\n", (std::size_t) src.get(), __FILE__, __LINE__); fflush(stdout);
797802
switch (policy) {
798803
case return_value_policy::automatic:
799804
case return_value_policy::automatic_reference:
@@ -835,8 +840,10 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
835840
inst_raw_ptr->owned = true;
836841
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
837842
valueptr = src_raw_void_ptr;
843+
printf("\nLOOOK [%lu] IsBase0 %s:%d\n", (std::size_t) valueptr, __FILE__, __LINE__); fflush(stdout);
844+
printf("\nLOOOK [%lu] IsDerived %s:%d\n", (std::size_t) st.first, __FILE__, __LINE__); fflush(stdout);
838845

839-
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src);
846+
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first)));
840847
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
841848

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

tests/test_mi_debug.py

Lines changed: 4 additions & 1 deletion
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)