[Numpy-discussion] Freeing memory allocated in C

Nick Fotopoulos nvf at MIT.EDU
Fri Apr 28 16:32:20 CDT 2006

Many thanks, with your help, I got it working without any leaks.  I  
need to run on ~10 TB of data, so fixing this leak sure helps my  
program scale.

One error in the code below is that PyString_FromFormat does not  
accept %f, so I created a regular string and created the PyString  
with PyString_FromString (it seems to copy data), then freed the  
regular string.  Is there any better way to do that?

I'm curious why I didn't see any explanation of PyArray_DATA in the  
NumPy book.  It seems really important, especially if you're touting  
it as the Proper Strategy.

Finally, Robert encouraged me to stop using the legacy interface.   
I'm happy to do so, but I have to cater to my users.  Approximately  
old a version of Numeric (and Numarray) will still work with  


On Apr 28, 2006, at 12:39 AM, Travis Oliphant wrote:

> The proper strategy for your arrays is to use PyArray_SimpleNew and  
> then get the data-pointer to fill using PyArray_DATA(...).  The  
> proper way to handle strings is to create a new string (say using  
> PyString_FromFormat)  and then return everything as objects.
> /* make sure shape is defined as intp unless you don't care about  
> 64-bit */
> obj2 = PyArray_SimpleNew(1, shape, PyArray_DOUBLE);
> data = (double *)PyArray_DATA(obj2)
> [snip...]
> out5 = PyString_FromFormat("Starting GPS time:%.1f UTC=%s",
>         vect->GTime,FrStrGTime(utc));
> return Py_BuildValue("(NNNdNNN)",out1,out2,out3,out4,out5,out6,out7);
> Make sure you use the 'N' tag so that another reference count isn't  
> generated.  The 'O' tag will increase the reference count of your  
> objects by one which is is not necessarily what you want (but  
> sometimes you do).

More information about the Numpy-discussion mailing list