在程序中調用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());
}
}