c# – 如何“修复”lambda表达式?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何“修复”lambda表达式?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3675字,纯文字阅读大概需要6分钟。
内容图文
![c# – 如何“修复”lambda表达式?](/upload/InfoBanner/zyjiaocheng/786/534d92d40e5840699fb723103b3ff67b.jpg)
我有一个方法,
public static void AddEventWatch(EventFilter filter)
{
SDL_AddEventWatch((IntPtr data, ref SDL_Event e) =>
{
filter(new Event(ref e));
return 0;
}, IntPtr.Zero);
}
这称为C函数,
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
internal static extern void SDL_AddEventWatch(SDL_EventFilter filter, IntPtr userData);
期待回调.
如上所示,我以lambda表达式的形式传递SDL_EventFilter,稍后由C API调用.
通过初步测试,这完全正常.我的理解是,lambda表达式可以被CLR垃圾收集器清理或者在内存中移动,因为它不知道DLL正在保存对它的引用.
>这是真的吗?
>如果是这样,我知道fixed关键字用于防止这种移动,
>如何将固定应用于代理?
>即使我’修复’它,它是否仍然不会被清理/删除,因为它已超出范围?
我做了一些实验.我打电话给GC.Collect();添加事件后,但在触发之前.它抛出一个CallbackOnCollectedDelegate异常,实际上比我期待的硬崩溃更令人愉快.
Darin’s solution似乎确实有效,但Marshal.GetFunctionPointerForDelegate步骤似乎是不必要的. C回调将使SDL_EventFilter正常,没有必要使它成为IntPtr.此外,通过GCHandle.ToIntPtr(gch)创建IntPtr实际上会在触发事件时导致崩溃.不确定为什么;似乎该方法是为此而构建的,它甚至在the MSDN example中使用.
达林与各州联系的文章:
Note that [the handle] need not be fixed at any specific memory location. Hence the version of GCHandle.Alloc() that takes a GCHandleType parameter :
06002
need not be used.
但是,MSDN没有说GCHandleType.Normal阻止回调被移动.实际上,.Pinned的描述如下:
This prevents the garbage collector from moving the object
所以我试了一下.它会导致带有帮助文本的ArgumentException:
Object contains non-primitive or non-blittable data
我只能希望这篇文章不是说它不需要固定,因为我不知道如何测试这种情况.
目前,这是我正在使用的解决方案:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate int SDL_EventFilter(IntPtr userData, ref SDL_Event @event);
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
internal static extern void SDL_AddEventWatch(SDL_EventFilter filter, IntPtr userData);
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_DelEventWatch")]
internal static extern void SDL_DelEventWatch(SDL_EventFilter filter, IntPtr userData);
public delegate void EventFilter(Event @event);
private static readonly Dictionary<EventFilter, Tuple<SDL_EventFilter, GCHandle>> _eventWatchers = new Dictionary<EventFilter, Tuple<SDL_EventFilter, GCHandle>>();
public static void AddEventWatch(EventFilter filter)
{
SDL_EventFilter ef = (IntPtr data, ref SDL_Event e) =>
{
filter(new Event(ref e));
return 0;
};
var gch = GCHandle.Alloc(ef);
_eventWatchers.Add(filter, Tuple.Create(ef,gch));
SDL_AddEventWatch(ef, IntPtr.Zero);
}
public static void DelEventWatch(EventFilter filter)
{
var tup = _eventWatchers[filter];
_eventWatchers.Remove(filter);
SDL_DelEventWatch(tup.Item1, IntPtr.Zero);
tup.Item2.Free();
}
但是,仅添加向字典添加ef可防止垃圾回收.我不确定GCHandle.Alloc是否会做任何事情.
解决方法:
1) Is this true?
是.
2) How do I apply fixed to a delegate?
像这样定义方法签名:
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
internal static extern void SDL_AddEventWatch(IntPtr filterPointer, IntPtr userData);
然后:
public static void AddEventWatch(EventFilter filter)
{
SDL_EventFilter myFilter = (IntPtr data, ref SDL_Event e) =>
{
filter(new Event(ref e));
return 0;
};
GCHandle gch = GCHandle.Alloc(myFilter);
try
{
var filterPointer = Marshal.GetFunctionPointerForDelegate(myFilter);
SDL_AddEventWatch(filterPointer, IntPtr.Zero);
}
finally
{
gch.Free();
}
}
基本上只要您将GCHandle保存在内存中,回调就不会被移动或GCed.
以下文章详细介绍:http://limbioliong.wordpress.com/2011/06/19/delegates-as-callbacks-part-2/
内容总结
以上是互联网集市为您收集整理的c# – 如何“修复”lambda表达式?全部内容,希望文章能够帮你解决c# – 如何“修复”lambda表达式?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。