我需要固定匿名代表吗?
我从 C# 应用程序调用 CopyFileEx,并将匿名委托传递到 LPPROGRESS_ROUTINE 参数中,以便获取有关文件复制进度的通知。
我的问题是,是否需要固定匿名委托以及为什么(或为什么不)。
此外,如果出现以下情况,答案是否会改变:
- CopyFileEx 没有阻塞。
- 如果我传入一个非匿名代表。
谢谢!
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:
- CopyFileEx was not blocking.
- If I passed in a delegate that was not anonymous.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
委托不需要被固定。如果垃圾收集器无法移动托管对象,则该对象将被固定。如果编组信息正确,则编组层将确保传递指向不可移动对象的指针。
然而,上面的评论表明局部变量可能会使委托保持活动状态,这表明对变量生命周期的误解。我建议您参阅规范,其中规定:
换句话说,如果您说:
那么允许抖动说“您知道,我看到没有托管代码再次使用 foo调用非托管调用后的那一刻;因此我可以在那时从另一个线程积极地回收该对象的存储”。这意味着当对象突然在另一个线程上释放时,非托管调用可能正在处理该对象。
如果 Foo 有析构函数,这尤其令人讨厌。当非托管库使用该对象时,终结代码可能会在另一个线程上运行,只有天知道这会导致什么样的灾难。
在这种情况下,您需要使用 KeepAlive 来保持托管对象的活动状态。不要依赖局部变量;局部变量被明确记录为不能保证保持活动状态。
请参阅 http://msdn.microsoft.com/en-us/ Library/system.gc.keepalive.aspx 了解更多详细信息。
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:
In other words, if you say:
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.
See http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx for more details.
您不需要固定它,但只要复制正在进行,您就需要保持对它的引用。
非托管代码调用的 thunk 被固定,但您必须确保委托没有被垃圾收集 - 因此是引用。
You don't need to pin it, but you do need to keep a reference to it alive as long as the copy is in progress.
The thunk that is called by the unmanaged code is pinned, but you have to make sure the delegate is not garbage collected - hence the reference.
从以下 msdn 看来固定和 GC.KeepAlive 都是在这种情况下不需要,因为 CopyFileEx 是同步的。具体来说,它说:
“通常,您不必担心委托的生命周期。每当您将委托传递给非托管代码时,CLR 都会确保该委托在调用期间处于活动状态。但是,如果本机代码保留超出调用范围的指针副本并打算稍后通过该指针回调,您可能需要使用 GCHandle 来显式阻止垃圾收集器收集委托。”
由于 CopyFileEx 不会在调用范围之外保留指向函数的指针,因此我们不需要调用 KeepAlive。
From the following msdn seems like both pinning and GC.KeepAlive are unneeded in this case since CopyFileEx is synchronous. Specifically it says:
"Usually, you won't have to worry about the lifetime of delegates. Whenever you are passing a delegate to unmanaged code, the CLR will make sure the delegate is alive during the call. However, if the native code keeps a copy of the pointer beyond the span of the call and intends to call back through that pointer later, you might need to use GCHandle to explicitly prevent the garbage collector from collecting the delegate."
Since CopyFileEx does not keep a pointer to the function beyond the span of the call, we shouldn't need to call KeepAlive.