[SciPy-User] [SciPy-user] numpy and C

Charles R Harris charlesr.harris@gmail....
Wed Jun 9 09:57:05 CDT 2010


On Wed, Jun 9, 2010 at 8:35 AM, tinauser <tinauser@libero.it> wrote:

>
> Dear Charles,
>
> thanks again for the replies.
> Why do you say that is difficoult to free memory?
> What I do is to allocate the memory(pyincref) before calling the Python
> script. The Python script uses then a timer to call a C function to which
> the allocated PyArrayObject (created with PyArray SimpleNew) is passed. In
> C, the pointer of the PyArray is assigned to a pointer that points to a
> sort
> of data buffer that is filled from a camera. The data buffer is allocated
> elsewhere.
> When the python GUI is closed, I just decref my PyArrayObject, that I'm
> basically using just to pass pointer values.
>
>
>
I don't know the details of your larger design, so perhaps my concerns are
irrelevant. The virtue of PyArray_SimpleNewFromData is that the array can be
deallocated without affecting the buffer memory.

PyArray SimpleNewFromData (PyObject*) (int nd, npy intp* dims,int typenum,
void* data)

Sometimes, you want to wrap memory allocated elsewhere into an ndarray
object
for downstream use. This routine makes it straightforward to do that. The
first three arguments are the same as in PyArray SimpleNew, the final
argument is a pointer to a block of contiguous memory that the ndarray
should use as it’s data-buffer which will be interpreted in C-style
contiguous
fashion. A new reference to an ndarray is returned, but the ndarray will not
own its data. When this ndarray is deallocated, the pointer will not be
freed.
You should ensure that the provided memory is not freed while the returned
array is in existence. The easiest way to handle this is if data comes from
another reference-counted Python object. The reference count on this object
should be increased after the pointer is passed in, and the base member of
the returned ndarray should point to the Python object that owns the data.
Then, when the ndarray is deallocated, the base-member will be DECREF’d
appropriately. If you want the memory to be freed as soon as the ndarray is
deallocated then simply set the OWNDATA flag on the returned ndarray.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100609/1deda824/attachment.html 


More information about the SciPy-User mailing list