执行方法时出错(.NET / COM) [英] Error on execution of a method (.NET / COM)

查看:97
本文介绍了执行方法时出错(.NET / COM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类中有一个Initialize(),我从这个函数调用一个委托包装器方法如下(回调方法将在一秒内调用大约20次),



I have one Initialize() in my class and I am calling a delegate wrapper method from this function as follows (the call back method will call around 20 times in a sec),

public Initialize()
{
                CX_OUTPUT_CALLBACK cb = new CX_OUTPUT_CALLBACK(OutDelegateCb);
                AureusSDK_Wrapper_VideoCallbackFunSetup.CX_SetOuputCallBack(cb, cnt);
}

public static void OutDelegateCb(IntPtr p_aureus_video_output, int num_of_people, IntPtr pObject)
{
 //Process the video...
}





当我执行方法时,我收到以下错误。你有没有人可以帮我解决这个问题:





I am getting the below error, when I execute the method. Could you anyone help me to resolve this issue:

A callback was made on a garbage collected delegate of type 'FaceTracker_Abc!AAA.VideoAnalytics.Framework.Implementations.FaceTracker_Abc.CX_OUTPUT_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 Initialize()
{
                CX_OUTPUT_CALLBACK cb = new CX_OUTPUT_CALLBACK(OutDelegateCb);
                AureusSDK_Wrapper_VideoCallbackFunSetup.CX_SetOuputCallBack(cb, cnt);
                GC.KeepAlive(cb);
}
 
public static void OutDelegateCb(IntPtr p_aureus_video_output, int num_of_people, IntPtr pObject)
{
 //Process the video...
}





这样可以防止垃圾收集收集你的对象。问题是你没有cb变量的引用(在COM之外)。由于垃圾收集是一个引用计数操作,当垃圾收集器运行时,它看到cb没有被引用到任何地方并且垃圾收集它。



That would prevent garbage collection from collecting your object. The problem is that you don't havea ny reference (outside of COM) to the "cb" variable. Since garbage collecting is a reference counting operation, when the garbage collector runs it sees the "cb" is not referenced anywhere and garbage collects it.


谢谢你,Ron Beyer ......



有效......:)
Thank you, Ron Beyer...

It worked...:)


这篇关于执行方法时出错(.NET / COM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆