Ctypes and freeing memory

Thomas Heller theller at python.net
Fri Oct 6 15:06:10 EDT 2006


Oliver Andrich schrieb:
> On 10/6/06, Thomas Heller <theller at python.net> wrote:
>> 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.
> 
> These is an even better solution, cause it lets me drop one decorator,
> I have been using at the moment, to handle these things.
> 
> Thanks a lot. ctypes is so much fun to use. Lately I had to use JNI,
> and well, compared to this experience I am now in heaven. :) Sadly, I
> can't provide my team members an equal solution in the Java world,
> cause one of them starts to hate me for assigning him on another JNI
> job in our project.

In the old ctypes CVS repository is a project started and sometimes
worked on by Bradley Schatz - ctypes-java.  I have never used it but
was amazed by the idea (!).  You could take a look at it or write
to Bradley ;-).

Thomas




More information about the Python-list mailing list