Ctypes and freeing memory

O

Oliver Andrich

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top