title: Pass pointer from C++ to Python /w boost python?
body: <p>I am using Boost Python, I generate a large vector of integers in C++, and I would like to access this vector in Python without copying it.</p>
  
  <p>In C++ I have:</p>
  
  <pre><code>BOOST_PYTHON_MODULE(myModule)
  {
      class_&lt;vector&lt;int&gt;&gt;("vectorInt").def(vector_indexing_suite&lt;vector&lt;int&gt;&gt;());
      def("ReturnVectorPtr", ReturnVectorPtr, return_value_policy&lt;manage_new_object&gt;());
  }
  
  vector&lt;int&gt;* ReturnVectorPtr()
  {
      return new vector&lt;int&gt;();
  }
  </code></pre>
  
  <p>Then in python I have:</p>
  
  <pre><code>import myModule
  myModule.ReturnVectorPtr()
  </code></pre>
  
  <p>This causes Python to crash, although I'm not even storing the return value.  Any ideas on what my mistake is?</p>
  
  <p>Edit:</p>
  
  <p>The following code works for getting the data in the vector from C++ to python, but leaks memory.  Are the vectors being copied and then not disposed?</p>
  
  <p>In C++:</p>
  
  <pre><code>BOOST_PYTHON_MODULE(myModule)
  {
      class_&lt;vector&lt;int&gt;&gt;("vectorInt").def(vector_indexing_suite&lt;vector&lt;int&gt;&gt;());
      def("ModifyVectorInPlace", ModifyVectorInPlace);
  }
  
  void ModifyVectorInPlace(vector&lt;int&gt;&amp; data)
  {
      // Modify data...
      return;
  }
  </code></pre>
  
  <p>Then in python I have:</p>
  
  <pre><code>import myModule
  vectorInt = myModule.vectorInt()
  myModule.ModifyVectorInPlace(vectorInt)
  </code></pre>
  
  <p>What is going on?</p>
  
  <p>Edit 2:</p>
  
  <p>I tried the "Raw C++ Pointers" example from here, exactly as written:
  <a href="https://wiki.python.org/moin/boost.python/PointersAndSmartPointers" rel="nofollow">https://wiki.python.org/moin/boost.python/PointersAndSmartPointers</a></p>
  
  <p>It crashes too.  It seems that I can't get a pointer to anything passed into Python for some reason...</p>
  
  <p>Edit 3:</p>
  
  <p>The crash appears to be a segfault from invoke.hpp, in this function:</p>
  
  <pre><code>template &lt;class RC, class F BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)&gt;
  inline PyObject* invoke(invoke_tag_&lt;false,false&gt;, RC const&amp; rc, F&amp; f BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, &amp; ac) )
  {
      return rc(f( BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT) ));
  }
  </code></pre>