ctypes free memory which is allocated in C DLL

zlchen.ken at gmail.com zlchen.ken at gmail.com
Sat Oct 27 10:42:01 EDT 2012


Hi Guys,

I have a DLL which written in C language, one of the function is to allocate a structure, fill the members and then return the pointer of the structure.

After Python called this function, and done with the returned structure, I would like to free the returned structure. How can I achieve this ? Basically, I tried that I wrote a corresponding free interface in the DLL, it works, but calling the libc.free in Python doesn't work.

my_dll.return_structure_ptr.restypes = POINTER(Dummy)
res_ptr = my_dll.return_structure_ptr()
windll.msvcrt.free(res_ptr)  <==== doesn't work, memory violation 
my_dll.free_dummy_struture(res_ptr)  <== This works.

Thanks !



More information about the Python-list mailing list