[C++-sig] create boost array from a Numpy PyArray without copying data?

Ravi lists_ravi at lavabit.com
Wed Jan 21 15:33:32 CET 2009

On Wednesday 21 January 2009 03:40:00 Sebastian Walter wrote:
> Apparently I have to increase the refcount manually when I return a raw
> PyObject. So I wrote getter functions like that:
>         PyObject* get_rhs(){
>                 return incref((PyObject *) _array_my_rhs);
>         }
> I'm not sure that this is the way it should be done. But it solved the
> memory corruption problem.

In this case, you are essentially creating a memory leak. The returned array 
will never be deleted by python since its reference count will never go to 
zero. Please see http://blog.enthought.com/?p=62 for information on fixing 
this issue. The code I pointed out earlier encapsulates this using a custom 
deleter in C++ which can be used independently of ublas.


More information about the Cplusplus-sig mailing list