Memory leak in python binding's libvlc_media_get_mrl
I think the python bindings leak memory when the underlying native function returns a char* that the caller is meant to free. The specific function I'm looking at is libvlc_media_get_mrl
which returns a char* that is allocated by strdup in input_item_GetURI
. There are probably others though.
I've attached a python script that will call get_mrl over and over again. While running this on either Ubuntu 10.8 amd64 or Debian Squeeze i386, I can watch the memory usage of the process climb steadily. I'm running VLC 1.1.4 on the Ubuntu machine and 1.1.3 on Debian, and I've tried using different versions of vlc.py including the latest generated one in git.
I've looked in vlc.py and I think the problem is that ctypes is told that the native function returns ctypes.c_char_p
, so ctypes will create a python string object copy of the string, but it has no idea that it should free the char*.
I've tried to hack my copy of vlc.py to import libc, change the return type of the function to ctypes.c_void_p, use ctypes.string_at() to create the python string before calling libc.free on the pointer. This appears to fix the leak in the simple script I attached but I'm getting segfaults in free in a more complicated app. Those might be related to other problems though, because I get different segfaults without the fix in other parts of the code.
I will continue working on my program to see if I can find the cause of these segfaults. Is there a better way to free these returned char* values with ctypes? I noticed libvlc_free, which could be more portable, but it's not in my version of VLC.
Plus, eventually the binding generator needs to be modified and needs a way to identify which functions need special treatment.