[SciPy-User] [SciPy-user] numpy and C
Charles R Harris
Wed Jun 9 09:57:05 CDT 2010
On Wed, Jun 9, 2010 at 8:35 AM, tinauser <email@example.com> 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
> of data buffer that is filled from a camera. The data buffer is allocated
> 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,
Sometimes, you want to wrap memory allocated elsewhere into an ndarray
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
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
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User