挺有意思的一个 bug,症状是运行了一小段时间之后,程序进程直接死掉,哪怕所有代码都用了 try...catch 都管不住。
故障发生的原因在于, [直接] 使用了实例方法当作非托管函数的回调函数参数使用。 类代码如下: public class SnapInfo { internal int CallBack_KaKou(IntPtr lAnalyzerHandle, uint dwEventType, IntPtr pEventInfo, IntPtr pBuffer, uint dwBufSize, IntPtr dwUser, int nSequence, IntPtr reserved) { ...... }
}
调用大华卡口摄像头抓拍照片并识别车牌的时候用的,调用 SDK 代码如下: NETClient.RealLoadPicture(loginId, ChannelNum, (uint)EM_EVENT_IVS_TYPE.TRAFFIC_MANUALSNAP, true, currentSnap.CallBack_KaKou, loginId, IntPtr.Zero);
其中 currentSnap 是 SnapInfo 类的实例。 执行一段时间之后,呵呵呵~~~
解决办法:
public class SnapInfo {
public SnapInfo(CameraDevice cameraDevice) { this.卡口回调 = new fAnalyzerDataCallBack(this.CallBack_KaKou); }
public fAnalyzerDataCallBack 卡口回调 { get; private set; }
internal int CallBack_KaKou(IntPtr lAnalyzerHandle, uint dwEventType, IntPtr pEventInfo, IntPtr pBuffer, uint dwBufSize, IntPtr dwUser, int nSequence, IntPtr reserved) { ...... }
}
把成员方法包装成成员属性,对非托管方法传参的时候替换为属性“卡口回调”,问题解决。
挺有趣的一个排错过程,记一下