在程序中调用C++链接库中的回调函由于没有考虑生命周期,直接写委托回随机的被gc给回收掉导致报这个错误
错误的程序:
private void InitPlateIdentify(){ try { if (string.IsNullOrEmpty(sPlateIP))return; handle = Dbvt_JpegCreateCamera(handle); Dbvt_JpegSetCameraCallBack(handle,DBVT_JpegSave,DBVT_GetSerialData,Dbvt_SetDevicesError); if (!Dbvt_JpegConnectDataCamera(handle, sPlateIP)) MessageBox.Show("打开车牌识别失败"); } catch (Exception ex) { MessageBox.Show("打开车牌识别失败"); Log.log("错误InitPlateIdentify:" + ex.ToString()); } }
正确的程序:
private CameraJpegCallBackFunc fMSGCallBack;private void InitPlateIdentify(){ try { if (string.IsNullOrEmpty(sPlateIP)) return; this.fMSGCallBack = this.DBVT_JpegSave; handle = Dbvt_JpegCreateCamera(handle); Dbvt_JpegSetCameraCallBack(handle, this.fMSGCallBack, DBVT_GetSerialData, Dbvt_SetDevicesError); if (!Dbvt_JpegConnectDataCamera(handle, sPlateIP)) MessageBox.Show("打开车牌识别失败"); } catch (Exception ex) { MessageBox.Show("打开车牌识别失败"); Log.log("错误InitPlateIdentify:" + ex.ToString()); }}