Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <list>
- #include <memory>
- #include <pybind11/pybind11.h>
- #include <pybind11/stl.h>
- struct B;
- struct A {
- A() {};
- ~A() {
- printf("~A()\n");
- if (!b_list.empty()) {
- printf("b_list not empty!\n");
- }
- };
- void register_b(B *b) {
- b_list.push_back(b);
- };
- void unregister_b(B *b) {
- auto removed = std::remove(b_list.begin(), b_list.end(), b);
- b_list.erase(removed, b_list.end());
- };
- std::list<B *> b_list;
- };
- struct B {
- B(A& a) : m_a(a) {
- m_a.register_b(this);
- };
- ~B() {
- printf("~B()\n");
- m_a.unregister_b(this);
- };
- A& m_a;
- };
- namespace py = pybind11;
- PYBIND11_MODULE(example, m) {
- py::class_<B>(m, "B");
- py::class_<A>(m, "A")
- .def(py::init<>())
- .def("get_b_list", [](A& _this) -> std::vector<B> {
- std::vector<B> b_vec;
- b_vec.push_back({_this});
- b_vec.push_back({_this});
- return b_vec;
- #if 1 // Switch between implementation described in the issue
- });
- #else
- }, py::return_value_policy::move, py::keep_alive<0, 1>());
- #endif
- }
Add Comment
Please, Sign In to add comment