pybind11/tests/test_class_sh_trampoline_shared_from_this.cpp
Ralf W. Grosse-Kunstleve bf8d6a2900 First fully successful attempt to make shared_from_this and trampolines play nicely.
Now also passes the open_spiel iterated_prisoners_dilemma_test ASAN clean, in addition to all pybind11 and PyCLIF unit tests.

The problem was that calling `std::shared_ptr<void>::reset()` with a `void` pointer cannot possibly update the `shared_from_this` `weak_ptr`.

The solution is to store a `shd_ptr_reset` function pointer in `guarded_deleter` (similar in idea to the stored function pointer for calling `delete`).

This commit still includes all debugging code, i.e. is "dirty". The code will be cleaned up after the GitHub CI is fully successful.
2021-06-30 07:04:31 -07:00

37 lines
958 B
C++

// Copyright (c) 2021 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "pybind11/smart_holder.h"
#include "pybind11_tests.h"
#include <memory>
#include <string>
namespace {
struct WithSft : std::enable_shared_from_this<WithSft> {
virtual ~WithSft() = default;
};
struct WithSftTrampoline : WithSft {
using WithSft::WithSft;
};
void pass_shared_ptr(const std::shared_ptr<WithSft> &obj) {
to_cout("LOOOK pass_shared_ptr entry");
to_cout("LOOOK obj->shared_from_this();");
obj->shared_from_this();
to_cout("LOOOK pass_shared_ptr return");
}
} // namespace
PYBIND11_SMART_HOLDER_TYPE_CASTERS(WithSft)
TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
py::classh<WithSft, WithSftTrampoline>(m, "WithSft").def(py::init<>());
m.def("pass_shared_ptr", pass_shared_ptr);
m.def("to_cout", to_cout);
}