我得到一个C库,需要一些回调, 他们在一个链表中处理。ctypes回调函数抛出SIGSEGV
蟒蛇调用的是那些:
def callback_exit():
print("exiting.")
sys.exit(0)
# never reached
return c_int(0)
def hw_print_int():
print("foo")
return c_int(0)
我将它们添加到列表中是这样的:
SFR_COMM=CFUNCTYPE(c_voidp)
class MyClass:
def add_SFR_callback(self,operation_number,callback):
all_callbacks=c_voidp.in_dll(self.memlib,"sfr_comms")
my_callback=self.memlib.newSFRCommand(c_uint(operation_number),callback)
new_all_callbacks=self.memlib.new_SFRCommandHolder(my_callback,all_callbacks)
self.memlib.set_sfr_comms(new_all_callbacks)
my_class_object.add_SFR_callback(0xff,SFR_COMM(callback_exit))
my_class_object.add_SFR_callback(0xff,SFR_COMM(hw_print_int))
这工作得很好,直到回调被调用,那么我只收到一个SIGSEGV。
重要:在SIGSEGV告诉我,这是一个“UngültigerMaschinenbefehl”(中译:无效处理器指令或类似的东西)
所以我不知道我该如何解决它。
这是C代码:
struct _SFRCommandHolder * sfr_comms;
#define DEBUG
unsigned int SpecialFunctionRegister_exec(unsigned int val)
{
struct _SFRCommandHolder * curr=sfr_comms;
unsigned int ret=-1;
while (curr!=NULL)
{
#ifdef DEBUG
printf("(%zd => %zd => %zd) %u ?= %u",curr,curr->com,curr->com->funct,curr->com->val,val);
#endif
if(curr->com->val==val)
{
#ifdef DEBUG
printf("\t\tTRUE\n");
#endif
ret=curr->com->funct(); // <= SIGSEGV here
#ifdef DEBUG
printf("callback done.\n");
#endif
}
#ifdef DEBUG
else
{
printf("\t\tFALSE\n");
}
#endif
curr=curr->next;
}
return ret;
}
我不认为,这是sys.exit
一个问题,因为它之前就好工作了几提交。
编辑: 调用hw_print_int
作品就好了,但callback_exit
不起作用。
顺便说一句:如果我不加hw_print_int
,callback_exit
作品,也
输出:
(13185760 => 13136448 => 139994994819144) 3 ?= 255 FALSE
(13038864 => 13034576 => 139994994819088) 255 ?= 255 TRUE
Ungültiger Maschinenbefehl (Speicherabzug geschrieben)
尝试初始化sfr_comms为NULL。 – cup
@cup不是问题。我的记忆处理是有效的。 (我测试过) – LittleByBlue