I am using Python Boost, I am generating a large integer vector in C ++, and I would like to access this vector in Python without copying it.
In C ++, I have:
BOOST_PYTHON_MODULE(myModule) { class_<vector<int>>("vectorInt").def(vector_indexing_suite<vector<int>>()); def("ReturnVectorPtr", ReturnVectorPtr, return_value_policy<manage_new_object>()); } vector<int>* ReturnVectorPtr() { return new vector<int>(); }
Then in python I have:
import myModule myModule.ReturnVectorPtr()
This causes Python to crash, although I don't even save the return value. Any ideas on what is my mistake?
Edit:
The following code works to get vector data from C ++ to python, but memory leaks. Are vectors overwritten and then not deleted?
In C ++:
BOOST_PYTHON_MODULE(myModule) { class_<vector<int>>("vectorInt").def(vector_indexing_suite<vector<int>>()); def("ModifyVectorInPlace", ModifyVectorInPlace); } void ModifyVectorInPlace(vector<int>& data) {
Then in python I have:
import myModule vectorInt = myModule.vectorInt() myModule.ModifyVectorInPlace(vectorInt)
What's happening?
Edit 2:
I tried the "Raw C ++ Pointers" example here, just like it says: https://wiki.python.org/moin/boost.python/PointersAndSmartPointers
Crash too. It seems that I cannot get a pointer to anything passed to Python for some reason ...
Edit 3:
The failure, apparently, is segfault from invoke.hpp, in this function:
template <class RC, class F BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)> inline PyObject* invoke(invoke_tag_<false,false>, RC const& rc, F& f BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, & ac) ) { return rc(f( BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT) )); }