我c#程序中
注册回调函数:public static extern int RegisterStreamDirectReadCallback_API(STREAM_DIRECT_READ_CALLBACK StreamDirectReadCallback, IntPtr Context);
回调函数:[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int STREAM_DIRECT_READ_CALLBACK(uint channelNumber, IntPtr DataBuf, uint Length, int FrameType, IntPtr context);
开启录像函数:public static extern int StartVideoCapture_API(IntPtr hChannelHandle);
在程序中首先调用"注册回调函数"RegisterStreamDirectReadCallback_API(STREAM_DIRECT_READ_CALLBACK StreamDirectReadCallback, IntPtr Context);,然后调用"开启录像函数"StartVideoCapture_API(IntPtr hChannelHandle);调用"开启录像函数"之后,只要收到数据系统就会通知"注册回调函数"对"回调函数"进行调用,但是现在我的程序中会随机的出现如下错误:
CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type '预览api实验!预览api实验.Form1+STREAM_DIRECT_READ_CALLBACK::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
我看帮助好像是与垃圾回收和托管封送有关系,但不知道具体如何解决,望各位高人不吝赐教,万分感激!!!!
注册回调函数:public static extern int RegisterStreamDirectReadCallback_API(STREAM_DIRECT_READ_CALLBACK StreamDirectReadCallback, IntPtr Context);
回调函数:[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int STREAM_DIRECT_READ_CALLBACK(uint channelNumber, IntPtr DataBuf, uint Length, int FrameType, IntPtr context);
开启录像函数:public static extern int StartVideoCapture_API(IntPtr hChannelHandle);
在程序中首先调用"注册回调函数"RegisterStreamDirectReadCallback_API(STREAM_DIRECT_READ_CALLBACK StreamDirectReadCallback, IntPtr Context);,然后调用"开启录像函数"StartVideoCapture_API(IntPtr hChannelHandle);调用"开启录像函数"之后,只要收到数据系统就会通知"注册回调函数"对"回调函数"进行调用,但是现在我的程序中会随机的出现如下错误:
CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type '预览api实验!预览api实验.Form1+STREAM_DIRECT_READ_CALLBACK::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
我看帮助好像是与垃圾回收和托管封送有关系,但不知道具体如何解决,望各位高人不吝赐教,万分感激!!!!
不知道您以前是否遇到过这样的情况,是怎么解决的啊!
回调函数:[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int STREAM_DIRECT_READ_CALLBACK(uint channelNumber, IntPtr DataBuf, uint Length, int FrameType, IntPtr context);
开启录像函数: StartVideoCapture_API(channelnum[1]);
回调函数体: public int callback_STREAM_DIRECT_READ_CALLBACK(uint channelNumber,IntPtr DataBuf, uint Length, int FrameType, IntPtr context)
{
............................
函数执行体
.................................
return 0;
}
主要就是如何确保新new出来的委托实例callback_STREAM_DIRECT_READ_CALLBACK的生存期覆盖非托管代码的生存期,让委托不会在执行之前不会被垃圾回收.
new delegate 的时间