python调用C++实现的动态链接库,崩溃于ffi_closure

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.

上一篇:《10分钟快速上手 数据库“硬核”挑战 》,完成挑战即可领取ApsaraDB定制版自动黑胶雨伞


下一篇:IDC:全球大数据和业务分析收入预计到2019年突破1870亿美元