我需要引脚匿名委托? [英] Do I need to pin an anonymous delegate?

查看:133
本文介绍了我需要引脚匿名委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个匿名委托C#应用程序调用CopyFileEx被传递到LPPROGRESS_ROUTINE参数,以获取有关文件的复制进度的通知。

I am calling CopyFileEx from a C# application with an anonymous delegate being passed into the LPPROGRESS_ROUTINE parameter in order to get notifications on the file copy progress.

我的问题是,并匿名委托需要被固定和为什么(或为什么不)

My question is, does the anonymous delegate need to be pinned and why (or why not).

此外,其答案是否变化,如果:

In addition, does the answer change if:


  1. CopyFileEx没有阻止。

  2. 如果我在一个委托,这不是匿名的通过。

谢谢!

推荐答案

委托并不需要是的固定的。管理对象是的固定的,如果它不能的移动的被垃圾收集器。如果在编组信息是正确的那么编组层将确保一个指针的东西不动的传递。

The delegate does not need to be pinned. A managed object is pinned if it cannot be moved by the garbage collector. If the marshalling information is correct then the marshalling layer will ensure that a pointer to something immobile is passed.

然而,上述在那里你认为一个局部变量可能保留意见委托的​​活着的指示变量一生的误解。我是指你的规范,其中规定:

However, the comment above where you suggest that a local variable might keep the delegate alive indicates a misunderstanding of variable lifetime. I refer you to the spec, which states:

局部变量的实际寿命是实现相关的。例如,一个编译器可能静态地确定,在一个块中的局部变量仅用于该块的一小部分。使用这种分析,编译器可以生成代码,其导致具有比其包含块更短的寿命的变量的存储。
局部引用变量所引用的存储的回收与该局部引用变量

The actual lifetime of a local variable is implementation-dependent. For example, a compiler might statically determine that a local variable in a block is only used for a small portion of that block. Using this analysis, the compiler could generate code that results in the variable’s storage having a shorter lifetime than its containing block. The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local reference variable

在其他单词的寿命,如果你说:

In other words, if you say:

void M()
{
    Foo foo = GetAFoo();
    UnmanagedLibrary.DoSomethingToFoo(foo);
}



然后抖动被允许说:你知道,我看到没有管理代码以后再使用富的那一刻起非托管的呼叫被调用后,所以我可以收回积极从另一个线程的,当时该对象的的存储。这意味着非托管的呼叫可以在对象上工作的时候,突然被另一个线程释放。

then the jitter is allowed to say "you know, I see that no managed code ever uses foo again the moment after the unmanaged call is invoked; I can therefore aggressively reclaim the storage of that object from another thread at that time". Which means that the unmanaged call can be working on the object when suddenly it is deallocated on another thread.

这是特别讨厌的,如果美孚具有析构函数。该终止代码将在另一个线程可能运行,而对象是使用非托管库,并且只有天知道什么样的灾难,将导致的。

This is particularly nasty if Foo has a destructor. The finalization code will possibly run on another thread while the object is in use by the unmanaged library, and heaven only knows what sort of disaster that will cause.

在这种情况下,你需要使用保持活动,以保持管理对象活着不要依赖于一个局部变量。局部变量是专门记录为不可以保证让事情活着。

In this circumstance you are required to use a KeepAlive to keep the managed object alive. Do not rely on a local variable; local variables are specifically documented as not guaranteed to keep things alive.

请参阅的http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx 了解更多详情。

See http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx for more details.

这篇关于我需要引脚匿名委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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