Ctypes and freeing memory

Thomas Heller theller at python.net
Fri Oct 6 12:45:09 EDT 2006


Oliver Andrich schrieb:
> Hi everybody,
> 
> I am currently playing around with ctypes and a C library (libWand
> from ImageMagick), and as I want to easily deploy it on Mac, Linux and
> Windows, I prefer a ctypes solution over a C module. At least on
> windows, I would have resource problems to compile the C module. So,
> ctypes is the perfect choice for me.
> 
> But I am currently encountering a pattern inside the C library, that
> has to be used to free memory. Coding in C I have to do the following
> 
>     char *description;
>     long  severity;
> 
>     description = MagickGetException(wand, &severity);
>     /*
>         do something with the description: print it, log it, ...
>     */
>     description = (char *) MagickRelinquishMemory(description);
>     exit(-1); /* or something else what I want to after an exception occured */
> 
> So, this looks easy and is sensible from C's point of view. Now I try
> to translate this to Python and ctypes.
> 
>     dll.MagickGetException.argtypes = [c_long, POINTER(c_long)]
>     dll.MagickGetException.restype  = c_char_p
> 
>     severity = c_long()
>     description = dll.MagickGetException(byref(severity))
> 
>     # So far so good. The above works like a charm, but the problem follows now
>     # ctypes already converted the char * for description to a String object.
>     # That means description has arrived in an area under Python's control.
> 
>     # these definitions are the easy part
>     dll.MagickRelinquishMemory.argtypes = [c_void_p]
>     dll.MagickRelinquishMemory.restype  = c_char_p
> 
>     # but this obviously must cause problems and causes problems
>     dll.MagickRelinquishMemory(description)
> 
> So, my question is, how do I deal with this situation? Can I ignore
> the call to MagickRelinquishMemory, cause Python takes care of the
> resources already? Or is it impossible to use it at all, and I have to
> think about a different solution?

ctypes only releases memory that it has allocated itself.  So, if you don't
call your MagickRelinquish function a memory leak will occur.

Chris Mellon has already pointed out a possible solution, but there is also a
different way.  You could use a subclass of c_char_p as the restype
attribute:

class String(c_char_p):
    def __del__(self):
        MagickRelinquishMemory(self)

The difference is that you will receive an instance of this class
when you call the MagickGetException funcion.  To access the 'char *'
value, you can access the .value attribute (which is inherited from
c_char_p), and to free the memory call MagickRelinquishMemory
in the __del__ method.

Thomas




More information about the Python-list mailing list