Ctypes and freeing memory

Oliver Andrich oliver.andrich at gmail.com
Tue Oct 3 05:19:14 EDT 2006


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?

Best regards,
Oliver

-- 
Oliver Andrich <oliver.andrich at gmail.com> --- http://roughbook.de/



More information about the Python-list mailing list