[Matrix-SIG] Freeing malloc'd array in Python

Aaron Watters aaron@cs.rutgers.edu
Sat, 05 Sep 1998 17:52:51 -0400


> Is there an easy way to free a malloc'd array from Python?

Sorry for the last message.

In general if you allocate memory and hand it to Python you
need to "wrap" the allocated structure in a python object with
its own "delete" function which does any finalization required
including deallocating the wrapped structure.  Please see either
of the current Python books for discussion.

I'm not acquainted with the C level of numpy but it might be that
what is happening in your case is that you allocate the array
and then *copy* it into a python array and then never deallocate it.
But I haven't looked into it.  If you just need fresh memory maybe
you could allocate a "blob" as a numpy array and then use the
internal buffer as storage, perhaps obviating the need to do a full
pyobject implementation... If you need to wrap preallocated
structures, then you gotta do the pyobject thing -- but be careful
if the structure is referenced outside of the "python domain".
Also maybe look into SWIG.
sorry again.
   -- Aaron Watters