在程序中调用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());}}
对“xxx”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃 损坏和数据丢失。向非托管代码传递委托时 托管应用程序必须让这些委托保持活动状态 直到确信不会再次调用它们。...