我正在用python开发一个程序。它使用C库,它使用SWIG链接到python。 C库是TCP服务器的一个王者,它在单独的C线程中处理客户端连接。这些线程从客户端接收数据,然后执行基本的传入数据处理和解析。之后,解析后的数据通过“Python函数回调”发送到python。然后在C中处理“Python函数回调”的结果并将其发送回客户端。
有一些段错误是由我的代码中的错误(双释放内存,NULL指针评估)引起的,我已经解决了。
但是现在我遇到了Python解释器代码中的段错误。我无法理解如何解决它。
我有一个使用gdb的回溯(为了清楚起见,省略了从5到11的堆栈帧):
#0 classify_argument (type=0x55, classes=0x0, byte_offset=byte_offset@entry=0) at ../src/x86/ffi64.c:158
#1 0x00007ffff4b4212c in examine_argument (type=<optimized out>, classes=classes@entry=0x7fffeb522c60, in_return=in_return@entry=false, pngpr=pngpr@entry=0x7fffeb522c58, pnsse=pnsse@entry=0x7fffeb522c5c) at ../src/x86/ffi64.c:314
#2 0x00007ffff4b4296f in ffi_closure_unix64_inner (closure=0x7ffff7ff20f0, rvalue=0x7fffe4024820, reg_args=0x7fffeb522cc0, argp=0x7fffeb522d90 "") at ../src/x86/ffi64.c:615
#3 0x00007ffff4b42de4 in ffi_closure_unix64 () at ../src/x86/unix64.S:229
#4 0x00007ffff5fd0b21 in MyCCodeConnectionHandler (parameter=0x7ffff7ff20f0, connection=0x7fffe4024820, event=SERVER_CONNECTION_CLOSED) at my_code/server.c:586
...
#12 0x00007ffff7bc4184 in start_thread (arg=0x7fffeb523700) at pthread_create.c:312
#13 0x00007ffff71e403d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
当我尝试在gdb中打印type-> type变量时,我看到:
(gdb) print type->type
Cannot access memory at address 0x5f
函数classify_argument的类型参数似乎无效,因为我知道从0x0000000000000000到0x000000000000FFFF的地址用于空指针检测。这就是SegFault的原因。
我已经使用断点调试了对函数classify_argument的其他调用,它向我展示了类型参数接收另一种值:
classify_argument (type=0x7ffff6662ee8, classes=0x1, byte_offset=byte_offset@entry=0) at ../src/x86/ffi64.c:158
classify_argument (type=0x7ffff65e7e10, classes=0x0, byte_offset=byte_offset@entry=0) at ../src/x86/ffi64.c:158
classify_argument (type=0x7ffff65e7e10, classes=0x0, byte_offset=byte_offset@entry=0) at ../src/x86/ffi64.c:158
而且没有任何段错误。
这是../src/x86/ffi64.c的一块:
155 static size_t
156 classify_argument (ffi_type *type, enum x86_64_reg_class classes[],
157 size_t byte_offset)
158 {
159 switch (type->type)
160 {
161 case FFI_TYPE_UINT8:
162 case FFI_TYPE_SINT8:
163 case FFI_TYPE_UINT16:
164 case FFI_TYPE_SINT16:
165 case FFI_TYPE_UINT32:
166 case FFI_TYPE_SINT32:
167 case FFI_TYPE_UINT64:
168 case FFI_TYPE_SINT64:
169 case FFI_TYPE_POINTER:
170 {
171 size_t size = byte_offset + type->size;
172
173 if (size <= 4)
174 {
175 classes[0] = X86_64_INTEGERSI_CLASS;
176 return 1;
177 }
178 else if (size <= 8)
179 {
180 classes[0] = X86_64_INTEGER_CLASS;
我知道在从C线程调用python代码之前需要获取的GIL锁。这是一段my_code / server.c
...
static void
MyCCodeConnectionHandler(void* parameter, ConnectionStruct *connection, ConnectionEvent event)
{
if(parameter != NULL) { //parameter is a pointer to Python callback
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
((void (*)(long, long))parameter)((long)connection, event);
PyGILState_Release(gstate);
}
}
...
参数参数在Python中具有以下TYPES定义
ConnectionHandlerFuncType = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_long)
并在传递给C之前使用此Python代码进行转换
def py_callback(connection, event):
# some code here
ctype_function_wrapper = ConnectionHandlerFuncType(py_callback)
cfunction_pointer = ctypes.cast(ctype_function_wrapper, ctypes.c_void_p).value
# cfunction_pointer is passed as parameter arg to MyCCodeConnectionHandler
请帮我解决这个段错误。
Directors解决了我的问题。看来这是在SWIG中进行回调的唯一正确方法。我将回调传递给C级的方式是错误的。
以下是我的C回调现在的样子:
static void
MyCCodeConnectionHandler(void* parameter, ConnectionStruct *connection, ConnectionEvent event)
{
//parameter contains (ConnectionCallbackDirector *) value casted to (void *)
if(parameter != NULL) {
PyGILState_STATE gstate = PyGILState_Ensure();
((ConnectionCallbackDirector *)parameter)->run(connection, event);
PyGILState_Release(gstate);
}
}
董事代码:
class ConnectionCallbackDirector {
public:
virtual ~ConnectionCallbackDirector() {}
virtual void run(ConnectionStruct *, ConnectionEvent) {}
};
我把它们放在了SWIG的* .i文件和enabled director feature for SWIG module and ConnectionCallbackDirector class
上。
在python中我写了这个继承自ConnectionCallbackDirector的类
class ConnectionCallback(my_c_lib.ConnectionCallbackDirector):
def __init__(self, pycallback):
my_c_lib.ConnectionCallbackDirector.__init__(self)
self._pycallback = pycallback
def run(self, connection, event):
return self._pycallback(connection, event)
并将从ConnectionCallback类创建的对象传递给C代码。
现在一切都很好!