ctypes & allocated memory

Michael Torrie torriem at gmail.com
Sun Jun 7 11:32:33 EDT 2020


On 6/7/20 7:15 AM, Miki Tebeka wrote:
> Hi,
> 
> 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?

I don't think so.  I did a quick google search and came up with this
discussion, which may be relevant:

https://stackoverflow.com/questions/13445568/python-ctypes-how-to-free-memory-getting-invalid-pointer-error

> 
> ---
> import ctypes
> 
> libc = ctypes.cdll.LoadLibrary('libc.so.6')
> strdup = libc.strdup
> strdup.argtypes = [ctypes.c_char_p]
> strdup.restype = ctypes.c_char_p
> 
> out = strdup(b'hello').decode('utf-8')
> print(out)  # hello
> del out
> ---
> 
> Thanks,
> Miki
> 



More information about the Python-list mailing list