GetComInterfaceForObject固定对象? [英] does GetComInterfaceForObject pin the object?

查看:72
本文介绍了GetComInterfaceForObject固定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否将使用GetComInterfaceForObject并将返回的IntPtr传递给非托管代码,以防止托管对象在内存中移动?还是clr会以某种方式维持该ptr?请注意,非托管代码将在程序的整个生命周期内使用它,并且我需要确保GC不会移动托管对象.(至少我认为是对的吗?)

Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed object from being moved in memory? Or does the clr somehow maintain that ptr? Note that the unmanaged code will use this for the lifetime of the program, and I need to make sure the managed object is not being moved by the GC.(At least I think that's right?)

  • 编辑-好吧,我找到了一些信息,我在想这可能是答案.它处理委托,但是我不得不相信调用GetComInterfaceForObject会执行相同的操作.

以下文字的来源

托管代表可以编组为非托管代码, 它们作为非托管函数指针公开.呼吁那些 指针将执行从非托管到托管的转换;改变 调用约定;输入正确的AppDomain;和任何必要的 论点封送.显然,非托管函数指针必须引用一个 固定地址.如果GC重新定位,那将是一场灾难!这 导致许多应用程序为委托创建一个固定句柄.这 完全没有必要.非托管函数指针实际上是指 到我们动态生成以执行过渡的本机代码存根 &封送.该存根存在于GC堆之外的固定内存中.

"Managed Delegates can be marshaled to unmanaged code, where they are exposed as unmanaged function pointers. Calls on those pointers will perform an unmanaged to managed transition; a change in calling convention; entry into the correct AppDomain; and any necessary argument marshaling. Clearly the unmanaged function pointer must refer to a fixed address. It would be a disaster if the GC were relocating that! This leads many applications to create a pinning handle for the delegate. This is completely unnecessary. The unmanaged function pointer actually refers to a native code stub that we dynamically generate to perform the transition & marshaling. This stub exists in fixed memory outside of the GC heap.

但是,应用程序负责以某种方式延长生命周期 代表,直到不再受非托管代码的调用为止.这 本机代码存根的生存期直接与代码存根的生存期相关 代表.收集到代表后,可以通过 非托管函数指针将崩溃或破坏进程.在 在我们的最新版本中,我们添加了一个客户调试探针,该探针可让您 干净地检测出代码中所有这些太常见的错误.如果没有 开始在开发过程中使用客户调试探针,请看看!"

However, the application is responsible for somehow extending the lifetime of the delegate until no more calls will occur from unmanaged code. The lifetime of the native code stub is directly related to the lifetime of the delegate. Once the delegate is collected, subsequent calls via the unmanaged function pointer will crash or otherwise corrupt the process. In our recent release, we added a Customer Debug Probe which allows you to cleanly detect this all too common bug in your code. If you havent started using Customer Debug Probes during development, please take a look!"

推荐答案

在您的编辑状态(关于委托)中,不需要固定受管对象,因为GetComInterfaceForObject返回一个固定"指针,该指针指向正确的管理对象.但是,您将需要确保托管对象的生存时间与COM客户端使用指向该对象的非托管指针的时间一样.

As your edit states (about delegates), your managed object doesn't need to be pinned, since GetComInterfaceForObject returns a "pinned" pointer that calls through to the correct managed object. However, you will need to make sure that the managed object lives for as long as the COM clients are using the unmanaged pointer to it.

这篇关于GetComInterfaceForObject固定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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