首先,抱歉我的英语不好。
我正在尝试从C线程接收异步回调,但每次调用这样的回调时python都会崩溃。
这是C回调原型:
/*! Type declaration for the frame ready callback function.
@sa IC_SetFrameReadyCallback
@sa IC_SetCallbacks
@retval IC_SUCCESS Callback was set successfully
@retval IC_ERROR An error occurred, e.g. hGrabber is NULL.*/
typedef void (*FRAME_READY_CALLBACK)
(HGRABBER hGrabber, unsigned char* pData, unsigned long frameNumber, void* );
这是注册回调的C函数:
/*! Enable frame ready callback.
@param hGrabber Handle to a grabber object.
@param cb Callback function of type FRAME_READY_CALLBACK
@param x1_argument_in_void_userdata Pointer to some userdata.
@retval IC_SUCCESS Callback was set successfully
@retval IC_ERROR An error occurred, e.g. hGrabber is NULL.
@sa FRAME_READY_CALLBACK*/
int AC IC_SetFrameReadyCallback(
HGRABBER hGrabber,
FRAME_READY_CALLBACK cb,
void* x1_argument_in_void_userdata);
最后,这是我的python代码的一部分:
FRAME_READY_CALLBACK = CFUNCTYPE(c_int, ICGrabber, POINTER(c_ubyte), c_ulong, c_void_p)
...
library = windll.LoadLibrary('tisgrabber_x64')
...
IC_SetFrameReadyCallback = library.IC_SetFrameReadyCallback
IC_SetFrameReadyCallback.restype = c_int
IC_SetFrameReadyCallback.argtypes = [ICGrabber, FRAME_READY_CALLBACK, c_void_p]
...
def frame_ready(icGrabber, data, frameNumber, unused):
print "oh"
#print "Frame", frameNumber, "ready!!!"
return 1
IC_InitLibrary(0) #library init function
...
IC_SetFrameReadyCallback(hGrabber, FRAME_READY_CALLBACK(frame_ready), None)
...
IC_SoftwareTrigger() #send signal
time.sleep(2) # while sleeping, the C thread calls the.. frame_ready callback
猜猜是什么?
在睡觉的时候,它崩溃了!
我正在谷歌搜索几个小时,但我找不到任何解决方案..
提前谢谢
答案 0 :(得分:2)
以及.. 我现在感觉很蠢,但是在我发布这个问题后几秒钟就出现了解决方案
这是错误的代码:
IC_SetFrameReadyCallback(hGrabber, FRAME_READY_CALLBACK(frame_ready), None)
这是工作中的一个:
cframe_ready = FRAME_READY_CALLBACK(frame_ready)
IC_SetFrameReadyCallback(grab, cframe_ready, None)
问题
我没有保留对回调的引用,所以python垃圾收集器清理它..