【发布时间】:2015-11-07 21:14:16
【问题描述】:
我有一个 c 库,需要一些回调, 它们在链表中处理。
python 可调用的是那些:
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ültiger Maschinenbefehl”(翻译:无效的处理器指令或类似的东西)
所以我只是不知道如何解决它。
这是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 这不是问题。我的记忆处理是有效的。 (我测试过)
标签: c python-3.x callback segmentation-fault shared-libraries