[Numpy-discussion] ANN: numpy.i - added managed deallocation to ARGOUTVIEW_ARRAY1 (ARGOUTVIEWM_ARRAY1)

Egor Zindy ezindy@gmail....
Sat Nov 15 21:11:06 CST 2008

Dear List,

after I tried to write a simple ARGOUTVIEW_ARRAY1 example (see 
), I started wondering about memory deallocation. Turns out a few clever 
people already did all the thinking (see http://blog.enthought.com/?p=62 
) and a few more clever people use this in a swig file (see 
http://niftilib.sourceforge.net/pynifti, file nifticlib.i).

All this concentrated knowledge helped me implement a single 
ARGOUTVIEWM_ARRAY1 fragment in numpy.i to do some testing (see attached 

How to use it? In yourfile.i, the %init function has a few lines added 
to it:

%init %{

   /* initialize the new Python type for memory deallocation */
   _MyDeallocType.tp_new = PyType_GenericNew;
   if (PyType_Ready(&_MyDeallocType) < 0)
       return; %}

... and that's it! then just use ARGOUTVIEWM_ARRAY1 instead of 
ARGOUTVIEW_ARRAY1 and python does the deallocation for you when the 
python array is destroyed (see the examples attached).

Everything compiles, but it would be nice to get rid of the warnings...

ezalloc_wrap.c:2763: warning: initialization from incompatible pointer type
ezalloc_wrap.c: In function `_wrap_alloc_managed':
ezalloc_wrap.c:2844: warning: assignment from incompatible pointer type
writing build\temp.win32-2.5\Release\_ezalloc.def

python setup_alloc.py build

The attached test_alloc.py does 2048 allocations of 1MB each for managed 
and unmanaged arrays. Output on my XP laptop with 1GB RAM as follows:

ARGOUTVIEWM_ARRAY1 (managed arrays) - 2048 allocations (1048576 bytes each)

ARGOUTVIEW_ARRAY1 (unmanaged, leaking) - 2048 allocations (1048576 bytes 
Step 482 failed

Find a better name for the methods (not sure I like ARGOUTVIEWM_ARRAY), 
then do the missing fragments (2D arrays), clear the warnings and verify 
the method for a possible inclusion in the official numpy.i file.

Thank you for reading!


-------------- next part --------------
A non-text attachment was scrubbed...
Name: ezalloc.zip
Type: application/x-zip-compressed
Size: 9133 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081116/4eb5e613/attachment.bin 

More information about the Numpy-discussion mailing list