On 6/7/20 2:25 PM, Barry wrote: >> Does ctypes, when using restype, frees allocated memory? >> >> For example, will the memory allocated by "strdup" be freed after the "del" >> statement? If not, how can I free it? > > See https://linux.die.net/man/3/strdup that tells you to use free() to delete > memory allocated by strdup. > > You must remember the result of strdup and free it at an appropriate time.
But the problem is that by specifying the type as ctypes.c_char_p, ctypes will hide that pointer from you and return a Python object instead. I'm not sure how ctypes is doing it under the hood, but I suspect ctypes is doing it's own strdup of the string on conversion, and managing that memory, but the original pointer ends up being lost and leaking memory. The stack exchange link I posted suggests that have ctypes give you a void * pointer, and then cast that to give you a python string while still having the original pointer to free. Is this correct? -- https://mail.python.org/mailman/listinfo/python-list