Dispose()方法清理托管资源? [英] Dispose() for cleaning up managed resources?

查看:189
本文介绍了Dispose()方法清理托管资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案我发现,

清理在Finalize方法的非托管资源和
在Dispose方法管理的人,当物/定稿模式
已经在你的代码中使用。

Cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code.

和后来我发现这个不错文章了解完成并处理,并得到关于他们一个清晰的概念。该项目具有以下代码(第3页),解释概念:

And later I found this nice article about finalize and dispose and got a clear idea about them. The article has the following code(Page 3), to explain the concepts:

      class Test : IDisposable
   {
     private bool isDisposed = false;

     ~Test()
     {
       Dispose(false);
     }

     protected void Dispose(bool disposing)
     {
       if (disposing)
       {
         // Code to dispose the managed resources of the class
       }
       // Code to dispose the un-managed resources of the class

       isDisposed = true;
     }

     public void Dispose()
     {
       Dispose(true);
       GC.SuppressFinalize(this);
     }
   }



但下面的是,相同的音符(我包括在这个问题的开始)出现。

But below that, the same note (which I included in the beginning of this question) appears.

的Dispose /最后确定模式
Microsoft建议您同时实现处置和完成时与非托管资源的工作。那么正确的顺序是
是开发人员调用Dispose。在敲定实施将
运行,当对象是收集即使开发商忽略显式调用Dispose
方法
垃圾的资源,仍然会被释放。弗朗切斯科Balena写在他的博客的
物/敲定模式,只有当你的类型,将调用分配非托管资源(包括非托管
内存)
非托管代码应该使用,并返回一个句柄,你必须使用最终释放
资源,两个处理,并通过调用他们的父母各自的方法完成必须链接到了其父
对象后,他们有
处置,或者自己的成员定稿。
简单地说,清理的Finalize方法的非托管资源,并在Dispose方法的管理的人,当
物/敲定格局已经在你的代码中使用。

The Dispose/Finalize Pattern Microsoft recommends that you implement both Dispose and Finalize when working with unmanaged resources. The correct sequence then would be for a developer to call Dispose. The Finalize implementation would run and the resources would still be released when the object is garbage collected even if a developer neglected to call the Dispose method explicitly. Francesco Balena writes in his blog "the Dispose/Finalize pattern should be used only when your type invokes unmanaged code that allocates unmanaged resources (including unmanaged memory) and returns a handle that you must use eventually to release the resource. Both dispose and finalize must chain up to their parent objects by calling their parent's respective methods after they have disposed or finalized their own members". Simply put, cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code.

现在我很迷茫一次。在整篇文章和代码示例中,它表明,非托管资源应被释放的Dispose()。但是,那么什么是该意见的相关性

Now I am confused again. In the entire article and in the code sample, it is shown that unmanaged resources should be freed in Dispose(). But then what is the relevance of that comment?

编辑:

由于它证实了这一行:

简单地说,清理的Finalize方法和$ b $的非托管资源b。在被管理的人处置方法,当物/敲定
格局已经在你的代码中使用

Simply put, cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code

是错误的,我主编的这个答案

推荐答案

请参阅它非常简单。


  1. 如果你正在处理非托管资源 - 同时实现的Dispose 和<强>敲定。处置是由开发商被要求,尽快为他们看到它,它不再需要为他们腾出的资源。如果他们忘记调用Dispose则框架调用最后确定了自己的GC周期(通常需要自己的甜蜜时光)。

  2. 如果你不使用非托管资源<处理/ STRONG> - 然后不要做任何事情。不落实敲定,也没有处置

  3. 如果你的对象使用一次性对象内部 - 你实现Dispose()方法,如果你创建并保持一个类型的任何对象的引用它实现了Dispose()方法和您尚未处置

  1. If you are dealing with unmanaged resources - Implement both Dispose and Finalize. Dispose is to be called by developers to free up the resources as soon as they see it that its no longer needed for them. If they forget to call the Dispose then Framework calls the finalize in its own GC cycle (usually will take its own sweet time).
  2. If you are NOT dealing with unmanaged resources- Then dont do anything. Dont implement Finalize nor Dispose.
  3. If your object uses Disposable objects internally - You implement Dispose() if you created and retained a reference to any object of a type which implements Dispose() and which you haven't already disposed.

一些经典的例子:

System.IO.FileStream 对象管理锁定/流处理的文件。因此,它同时实现了部署和落实。如果开发人员部署它,然后其他程序可以访问它的时候了。如果他忘了处理掉它,然后框架定稿,后来关闭手柄在GC周期。

System.IO.FileStream object manages the lock/stream handles to files. So it implements both dispose and finalize. If the developer disposes it then the other program can access it right away. If he forgets to dispose it then Framework finalize it and close the handles later in its GC cycle.

System.Text.StringBuilder 剂量没有任何非托管资源。因此,没有处置没有敲定。

System.Text.StringBuilder dose not have any unmanaged resource. So no dispose no finalize.

至于格局而言意味着什么
//代码处置托管资源类
是打电话,你必须为该类

As far as the pattern is concerned what it means to
// Code to dispose the managed resources of the class
is that call the Dispose methods of any .NET objects that you have as components inside that class

和内部组件的任何.NET对象的Dispose方法

And

//代码处置类的非托管资源
指收生手柄和指针。这里是一个例子你更新的代码

// Code to dispose the un-managed resources of the class Means to close the raw handles and pointers. Here is your updated code with examples

class Test : IDisposable
   {
     private bool isDisposed = false;

     ~Test()
     {
       Dispose(false);
     }

     protected void Dispose(bool disposing)
     {
       if (disposing)
       {
         // Code to dispose the managed resources of the class
         internalComponent1.Dispose();
         internalComponent2.Dispose();
       }

       // Code to dispose the un-managed resources of the class
       CloseHandle(handle);
       handle = IntPtr.Zero;   

       isDisposed = true;
     }

     public void Dispose()
     {
       Dispose(true);
       GC.SuppressFinalize(this);
     }
   }

下面一个老问题explaing它的http://social.msdn.microsoft.com/Forums/en-US / csharplanguage /线程/ 186947fd-e6da-41fb-9998-3b1e6223f17c

Here an old question explaing it http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/186947fd-e6da-41fb-9998-3b1e6223f17c

这篇关于Dispose()方法清理托管资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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