By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Ctypes and freeing memory

P: n/a
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 <ol************@gmail.com--- http://roughbook.de/
Oct 3 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.