castes:
#include <boost/python.hpp> using namespace boost; using namespace boost::python; struct Foo { virtual ~Foo() {} virtual void Print() = 0; }; struct FooWrap : Foo, wrapper<Foo> { void Print() { this->get_override("Print")(); } }; void ProcessFoo(Foo *obj) { obj->Print(); } BOOST_PYTHON_MODULE(hello_ext) { class_<FooWrap, boost::noncopyable>("Foo") .def("Print", pure_virtual(&Foo::Print)); def("ProcessFoo", &ProcessFoo); }
python:
import hello_ext class NewFoo(hello_ext.Foo): def Print(self): print 'Print call' hello_ext.ProcessFoo( NewFoo() )
Everything works fine, there is the text Print call from the ProcessFoo call. But I want to save all passed pointers to ProcessFoo as follows:
std::vector<Foo*> data; void ProcessFoo(Foo *obj) { data.push_back(obj); obj->Print(); }
After exiting the function, the pointer becomes invalid, and I cannot use it from the vector. What is the best way to extend the life of this pointer? Use shared pointers or tell python not to delete the object (if it deletes it?)
source share