JohannesBuchner/PyMultiNest — Issue #5: python/ctypes segfault in ffi_closure_unix64_inner when callback function is called
Author: JohannesBuchner
Created: 2012-02-26 15:22:19+00:00
State: open

I created this issue to help people who face the same difficulty.

If you write a python / C interface with ctypes, and use callbacks: If you don't keep a reference to your callback function, it can get garbage collected (mentioned at the end of http://python.net/crew/theller/ctypes/tutorial.html#callback-functions).

You will get a segfault in ffi_closure_unix64_inner just like here:

$ gdb python
> run
Program received signal SIGSEGV, Segmentation fault.
0x0000003fc9a05cd1 in ffi_closure_unix64_inner () from /usr/lib64/libffi.so.5
$

so do

cmp_func = CMPFUNC(py_cmp_func)
qsort(ia, len(ia), sizeof(c_int), cmp_func)

instead of

qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func))

Hope this helped you.