On 10/3/06, Oliver Andrich <oliver.andrich at gmail.com> wrote:
> 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?
>
Not a ctypes expert but I can pretty much guaranteee that Python won't
correctly release that memory by itself. Are you sure that description
is actually a string object?
By my recollection it should be a c_char_p object, and if you pass it
to MagickRelinquishMemory ctypes should do the right thing.
If it doesn't, then you can use a more literal translation of the C
code, and use a c_void_p instead, using ctypes.string_at(description)
to get a string out of the buffer.
> Best regards,
> Oliver
>> --
> Oliver Andrich <oliver.andrich at gmail.com> --- http://roughbook.de/> --
>http://mail.python.org/mailman/listinfo/python-list>