[Numpy-discussion] Owndata flag

Gael Varoquaux gael.varoquaux at normalesup.org
Fri Dec 23 04:01:36 EST 2011


On Thu, Dec 15, 2011 at 04:36:24PM +0000, Robert Kern wrote:
> > More explicitly, I have some temporary home-made C structure that holds
> > a pointer to an array. I prepare (using Cython) an numpy.ndarray using
> > the PyArray_NewFromDescr function. I can delete my temporary C structure
> > without freeing the memory holding array, but I wish the numpy.ndarray
> > becomes the owner of the data.

> > How can do I do such thing ?

> You can't, really. numpy-owned arrays will be deallocated with numpy's
> deallocator. This may not be the appropriate deallocator for memory
> that your library allocated.

Coming late to the battle, but I recently followed the same route, and
came to similar conclusions: using the owndata flag is not suited, and
you will need you own deallocator.

I implemented a demo code showing all the steps to implement this
strategy to bind an existing C library with Cython in
https://gist.github.com/1249305
in particular, the deallocator is in
https://gist.github.com/1249305#file_cython_wrapper.pyx

I hope that this code sample is usefull.

Gael



More information about the NumPy-Discussion mailing list