
Hello everyone. I'm trying to map into python classes shown below. class A { friend class B; public: ~A(void) {} private: A(B& b) : m_B(&b) { } B* m_B; }; class B { friend class C; public: ~B(void) {} A* make_a(void) { return new A(*this); } private: B(C& c) : m_C(&c) { } C* m_C; }; class C { public: ~C(void) {} B* make_b(void) { return new B(*this); } }; C* make_c() { return new C(); } Objects are supposed to be created in the order C, B, A and destroyed in the order A, B, C. I do not want to map constructors of A, B and C. I can see three ways to do what I want: 1) class_<A>("A", no_init); class_<B>("B", no_init) .def("make_a", &B::make_a, return_internal_reference<>()) ; class_<C>("C", no_init) .def("make_b", &C::make_b, return_internal_reference<>()) ; def("make_c", make_c, return_value_policy<manage_new_object>()); 2) class_<A>("A", no_init); class_<B>("B", no_init) .def("make_a", &B::make_a, return_value_policy<manage_new_object>()) ; class_<C>("C", no_init) .def("make_b", &C::make_b, return_value_policy<manage_new_object>()) ; def("make_c", make_c, return_value_policy<manage_new_object>()); 3) class_<A, shared_ptr<A>, noncopyable>("A", no_init); class_<B, shared_ptr<B>, noncopyable>("B", no_init) .def("make_a", &B::make_a, return_value_policy<manage_new_object>()) ; class_<C, shared_ptr<C>, noncopyable>("C", no_init) .def("make_b", &C::make_b, return_value_policy<manage_new_object>()) ; def("make_c", make_c, return_value_policy<manage_new_object>()); C::make_b and B::make_a are supposed to return shared_ptr<B> and shared_ptr<A> respectively in this case. After debugging this code I've got strange results: - In case 1 objects aren't get deleted at all; - In case 2 objects are get deleted in wrong order; - In case 3 objects are get deleted in wrong order; Cases 2 and three seem to be identical to me. The only difference I can see is that in case 3 I should manage objects lifetime by myself from C++ code. In opposite, in case 2 objects lifetime will be managed by python. I couldn't get an answer to my problem neither from documentation nor from Boost.Python tests nor from Google. Any explanations or suggestions will be greatly appreciated. Thank you. -- Sergey Sikorskiy