swig python - segfault with callbacks

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

swig python - segfault with callbacks

denny khoerniawan
Hi All,

I just started using SWIG to generate APIs for various languages from existing C APIs and I am starting with Python as the first language. Almost everything works very well but the callbacks that I implemented following the instruction from SWIG documentations. There are 2 callback functions that I am having issue with here and here are the wrappers that I declared in my interface file.


static void state_callback_py( state_t new_state, state_t old_state, void* user_data )
{
PyObject *func = NULL;
PyObject *arglist = NULL;
PyObject *result = NULL;

func = (PyObject *) user_data;
arglist = Py_BuildValue("(ii)", (int)new_state, (int)old_state);
result = PyEval_CallObject(func,arglist);
Py_XDECREF(arglist);
Py_XDECREF(result);
}

static void log_callback_py( log_level_t log_level, const char* msg, void* user_data )
{
PyObject *func = NULL;
PyObject *arglist = NULL;
PyObject *result = NULL;

func = (PyObject *) user_data;
arglist = Py_BuildValue("(is)", log_level, msg);
result = PyEval_CallObject(func,arglist);
Py_XDECREF(arglist);
Py_XDECREF(result);
}

status_t state_callback_py( agent_t* lib_handle, PyObject *pyfunc )
{
status_t status;
status = state_callback( lib_handle, state_callback_py, (void *) pyfunc );
Py_INCREF(pyfunc);
return status;
}

status_t log_callback_py( agent_t* lib_handle, PyObject *pyfunc )
{
status_t status;
status = log_callback( lib_handle, log_callback_py, (void *) pyfunc );
Py_INCREF(pyfunc);
return status;
}

I get segfault almost all the time with state_callback_py() (> 90% of the time) and occasionally get it with log_callback_py() (< 1% of the time). and here are the gdb's backtrace for both cases.

for state_callback_py():
#0  0x4a71047c in PyObject_Call () from /usr/lib/libpython2.7.so.1.0
#1  0x4a7a883d in PyEval_CallObjectWithKeywords () from /usr/lib/libpython2.7.so.1.0
#2  0xb7ed5468 in state_callback_py (new_state=TATE_MAINTENANCE, old_state=STATE_NORMAL, 
    user_data=0xb7f5502c)
    at /buildarea2/dkhoerni/quark/bitbake_build/tmp/work/quark-wrs-linux/hdc/1.0-r5/cmake_build/src/agent-api/common_pythonPYTHON_wrap.c:3378
#3  0xb7eb8086 in common_notification_thread (arg=0xb7ec6400 <lib_handle>)
    at /buildarea2/dkhoerni/quark/bitbake_build/tmp/work/quark-wrs-linux/hdc/1.0-r5/src/agent-api/common.c:193
#4  0x4a38a06b in ?? () from /lib/libpthread.so.0
#5  0x4a2d365e in clone () from /lib/libc.so.6


for log_callback_py()
#0  0x4a71047c in PyObject_Call () from /usr/lib/libpython2.7.so.1.0
#1  0x4a7a883d in PyEval_CallObjectWithKeywords () from /usr/lib/libpython2.7.so.1.0
#2  0xb7d174f8 in log_callback_py (log_level=LOG_TRACE, 
    msg=0xa1afdee0 "Handling registration acknowledgement", user_data=0xb7f5c1e4)
    at /buildarea2/dkhoerni/quark/bitbake_build/tmp/work/quark-wrs-linux/hdc/1.0-r5/cmake_build/src/agent-api/common_pythonPYTHON_wrap.c:3392
#3  0xb7cf9d41 in log (lib_handle=0xb7d08400 <lib_handle>, log_level=LOG_TRACE, 
    log_msg_fmt=0xb7d01d58 "Handling registration acknowledgement")
    at /buildarea2/dkhoerni/quark/bitbake_build/tmp/work/quark-wrs-linux/hdc/1.0-r5/src/agent-api/common.c:467
#4  0xb7cfa0de in common_notification_thread (arg=0xb7d08400 <lib_handle>)
    at /buildarea2/dkhoerni/quark/bitbake_build/tmp/work/quark-wrs-linux/hdc/1.0-r5/src/agent-api/common.c:170
#5  0x4a38a06b in ?? () from /lib/libpthread.so.0
#6  0x4a2d365e in clone () from /lib/libc.so.6

Can someone help me explain what is going on here? I just find it really weird that those 2 functions are implemented almost exactly the same way, but behave differently. and why is it intermittent? I have more callbacks that are called less often and I suspect they will have the same issue. Am I missing something here? I have been struggling with this for over a week now, I would really appreciate any help I can get.

Thanks,
Denny


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user