<small id='Lqp9m'></small><noframes id='Lqp9m'>

    1. <i id='Lqp9m'><tr id='Lqp9m'><dt id='Lqp9m'><q id='Lqp9m'><span id='Lqp9m'><b id='Lqp9m'><form id='Lqp9m'><ins id='Lqp9m'></ins><ul id='Lqp9m'></ul><sub id='Lqp9m'></sub></form><legend id='Lqp9m'></legend><bdo id='Lqp9m'><pre id='Lqp9m'><center id='Lqp9m'></center></pre></bdo></b><th id='Lqp9m'></th></span></q></dt></tr></i><div id='Lqp9m'><tfoot id='Lqp9m'></tfoot><dl id='Lqp9m'><fieldset id='Lqp9m'></fieldset></dl></div>

      <legend id='Lqp9m'><style id='Lqp9m'><dir id='Lqp9m'><q id='Lqp9m'></q></dir></style></legend>

      <tfoot id='Lqp9m'></tfoot>
          <bdo id='Lqp9m'></bdo><ul id='Lqp9m'></ul>
      1. 从终结器调用 RCW 是否安全?

        时间:2023-07-27
          <tbody id='BEA55'></tbody>

        1. <small id='BEA55'></small><noframes id='BEA55'>

            <bdo id='BEA55'></bdo><ul id='BEA55'></ul>

                <tfoot id='BEA55'></tfoot><legend id='BEA55'><style id='BEA55'><dir id='BEA55'><q id='BEA55'></q></dir></style></legend>
                  <i id='BEA55'><tr id='BEA55'><dt id='BEA55'><q id='BEA55'><span id='BEA55'><b id='BEA55'><form id='BEA55'><ins id='BEA55'></ins><ul id='BEA55'></ul><sub id='BEA55'></sub></form><legend id='BEA55'></legend><bdo id='BEA55'><pre id='BEA55'><center id='BEA55'></center></pre></bdo></b><th id='BEA55'></th></span></q></dt></tr></i><div id='BEA55'><tfoot id='BEA55'></tfoot><dl id='BEA55'><fieldset id='BEA55'></fieldset></dl></div>
                • 本文介绍了从终结器调用 RCW 是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个托管对象,它调用 COM 服务器来分配一些内存.托管对象必须在托管对象消失之前再次调用 COM 服务器以释放该内存以避免内存泄漏.此对象实现 IDisposable 以帮助确保进行正确的内存释放 COM 调用.

                  I have a managed object that calls a COM server to allocate some memory. The managed object must call the COM server again to free that memory before the managed object goes away to avoid a memory leak. This object implements IDisposable to help ensure that the correct memory-releasing COM call is made.

                  如果Dispose 方法被 调用,我希望对象的终结器释放内存.问题是,终结的规则是你不能访问任何引用,因为你不知道在你之前已经被 GC 和/或终结的其他对象.这使得唯一可触摸的对象状态是字段(句柄是最常见的).

                  In the event that the Dispose method is not called, I would like the object's finalizer to free the memory. The trouble is, the rules of finalization is that you must not access any reference because you don't know what other objects have already been GC'd and/or finalized before you. This leaves the only touchable object state to be fields (handles being the most common).

                  但是调用 COM 服务器需要通过运行时可调用包装器 (RCW) 来释放内存,因为我有一个 cookie 来存储在一个字段中.从终结器调用 RCW 是否安全(是否保证此时尚未被 GC 或终结)?

                  But calling a COM server involves going through an runtime callable wrapper (RCW) in order to free the memory that I have a cookie to stored in a field. Is that RCW safe to call from a finalizer (is it guaranteed to have not been GC'd or finalized at this point)?

                  对于那些不熟悉终结的人,虽然终结器线程在运行时在托管 appdomain 的后台运行,但对于这些情况,触摸引用理论上是可以的,终结也发生在 appdomain 关闭时,并且 以任何顺序 -- 不仅仅是参考关系顺序.这限制了您可以假设从终结器中可以安全触摸的内容.任何对托管对象的引用都可能是坏的"(收集的内存),即使该引用是非空的.

                  For those of you not familiar with finalization, although the finalizer thread runs in the background of a managed appdomain while its running, at for those cases touching references would theoretically be OK, finalization also happens at appdomain shutdown, and in any order -- not just in reference relationship order. This limits what you can assume is safe to touch from your finalizer. Any reference to a managed object might be "bad" (collected memory) even though the reference is non-null.

                  更新:我刚刚试了一下,得到了这个:

                  Update: I just tried it and got this:

                  myassembly.dll 中出现System.Runtime.InteropServices.InvalidComObjectException"类型的未处理异常

                  An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in myassembly.dll

                  附加信息:已与其底层 RCW 分离的 COM 对象无法使用.

                  Additional information: COM object that has been separated from its underlying RCW cannot be used.

                  推荐答案

                  我从 CLR 团队自己发现这确实不安全 -- 除非 你在 RCW 上分配 GCHandle 而它是这样做仍然安全(当您第一次获得 RCW 时).这确保了在需要调用它的托管对象完成之前,GC 和终结器没有总计 RCW.

                  I found out from the CLR team themselves that indeed it is not safe -- unless you allocate a GCHandle on the RCW while it's still safe to do so (when you first acquire the RCW). This ensures that the GC and finalizer haven't totaled the RCW before the managed object that needs to invoke it is finalized.

                  class MyManagedObject : IDisposable
                  {
                      private ISomeObject comServer;
                      private GCHandle rcwHandle;
                      private IServiceProvider serviceProvider;
                      private uint cookie;
                  
                      public MyManagedObject(IServiceProvider serviceProvider)
                      {
                          this.serviceProvider = serviceProvider;
                          this.comServer = this. serviceProvider.GetService(/*some service*/) as ISomeObject;
                          this.rcwHandle = GCHandle.Alloc(this.comServer, GCHandleType.Normal);
                          this.cookie = comServer.GetCookie();
                      }
                  
                      ~MyManagedObject()
                      {
                          this.Dispose(false);
                      }
                  
                      public void Dispose()
                      {
                          this.Dispose(true);
                          GC.SuppressFinalize(this);
                      }
                  
                      protected virtual void Dispose(bool disposing)
                      {
                          if (disposing)
                          {
                              // dispose owned managed objects here.
                          }
                  
                          if (this.rcwHandle.IsAllocated)
                          {
                              // calling this RCW is safe because we have a GC handle to it.
                              this.comServer.ReleaseCookie(this.cookie);
                  
                              // Now release the GC handle on the RCW so it can be freed as well
                              this.rcwHandle.Free();
                          }
                      }
                  }
                  

                  事实证明,在我的特殊案例中,我的应用正在托管 CLR 本身.因此,它在终结器线程开始运行之前调用 mscoree!CoEEShutdownCOM,这会杀死 RCW 并导致我看到的 InvalidComObjectException 错误.

                  It turns out in my particular case, my app is hosting the CLR itself. Therefore, it's calling mscoree!CoEEShutdownCOM before the finalizer thread gets to run, which kills the RCW and results in the InvalidComObjectException error I was seeing.

                  但在 CLR 不自行托管的正常情况下,我被告知这应该可以工作.

                  But in normal cases where the CLR is not hosting itself, I'm told this should work.

                  这篇关于从终结器调用 RCW 是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何 UAC 使用 .NET 提升 COM 组件 下一篇:Shell32.Folder.GetDetailsOf(..,..) 有哪些可用选项?

                  相关文章

                  1. <tfoot id='OFZSN'></tfoot>

                    <small id='OFZSN'></small><noframes id='OFZSN'>

                    <legend id='OFZSN'><style id='OFZSN'><dir id='OFZSN'><q id='OFZSN'></q></dir></style></legend>
                      • <bdo id='OFZSN'></bdo><ul id='OFZSN'></ul>

                    1. <i id='OFZSN'><tr id='OFZSN'><dt id='OFZSN'><q id='OFZSN'><span id='OFZSN'><b id='OFZSN'><form id='OFZSN'><ins id='OFZSN'></ins><ul id='OFZSN'></ul><sub id='OFZSN'></sub></form><legend id='OFZSN'></legend><bdo id='OFZSN'><pre id='OFZSN'><center id='OFZSN'></center></pre></bdo></b><th id='OFZSN'></th></span></q></dt></tr></i><div id='OFZSN'><tfoot id='OFZSN'></tfoot><dl id='OFZSN'><fieldset id='OFZSN'></fieldset></dl></div>