English 中文(简体)
对于继承可转让的微软建筑类别,我是否明确要求放弃?
原标题:For Microsoft built classes that inherit IDisposable, do I explicitly have to call Dispose?

关于Microsoft建造的、可继承IDis的类别,我是否明确要求放弃以避免记忆泄露?

我理解说,最好的做法是称之为处置(或更好使用使用一环),然而,在拟定方案时,通常我不会立即认识到某类人继承了可开发的保有权。

我还理解,Microsoft执行可转让的识别器是一种借方,这就是为什么会创建的正确使用。

长期短篇故事,在什么情况下,忘记叫弃吗?

最佳回答
问题回答

主要问题有几个问题。

Do I explicitly have to call Dispose to prevent memory leaks?

电话Disposeany 执行<代码>可转让<>代码>的类型高度重复,甚至可能是类型合同的基本部分。 在你与标的做成时,几乎没有理由不打上<代码>Dispose。 拟处理<代码>可支配物体。

But will failing to call Dispose create a memory leak? Possibly. It s very dependent on what exactly that object does in it s Dispose method. Many free memory, some unhook from events, others free handles, etc ... It may not leak memory but it will almost certainly have a negative effect on your program

在什么情况下,人们忘记放弃吗?

我首先没有。 绝大多数物体出于良好原因执行<代码> 可支配。 如果不打电话<代码>,Dispose,就会损害你的方案。

http://www.ohchr.org。

A good 95% of IEnumerator<T> implementations have a Dispose that s safe to ignore, but the 5% is not just 5% that ll cause a bug, but 5% that ll cause a nasty hard to trace bug. More to the point, code that gets passed an IEnumerator<T> will see both the 95% and the 5% and won t be able to dynamically tell them apart (it s possible to implement the non-generic IEnumerable without implementing IDisposable, and how well that turned out can be guessed at by MS deciding to make IEnumerator<T> inherit from IDisposable!).

在其余部分中,可能还有3或4倍的安全时间。 现在。 你们不知道哪3%没有看过该法典,甚至当时合同说,你必须称呼该守则,这样,如果开出一个新版本的话,开发商可以依靠你这样做。

In summary, always call Dispose(). (I can think of an exception, but it s frankly too weird to even go into the details of, and it s still safe to call it in that case, just not vital).

关于实施问题: IDisposable yourself, a Without the patterns in that accursed document .

我认为这种模式是一种反家长。 这是实施<代码>可转让的两种格式。 弃置,在既管理又管理着资源的类别中作最后处理。 然而,持有经过管理的<代码>IDisposable和未经管理的资源,首先是坏账。

相反:

如果你拥有未经管理的资源,那么没有管理的资源来执行<代码>。 IDisposable。 如今,<代码>Dispose(true)和Dispose(false)的编码途径相同,因此它们实际上可以:

public class HasUnmanaged : IDisposable
{
  IntPtr unmanagedGoo;
  private void CleanUp()
  {
    if(unmanagedGoo != IntPtr.Zero)
    {
      SomeReleasingMethod(unmanagedGoo);
      unmanagedGoo = IntPtr.Zero;
    }
  }
  public void Dispose()
  {
    CleanUp();
    GC.SuppressFinalize(this);
  }
  ~HasUnmanaged()
  {
    CleanUp();
  }
}

如果你管理了需要处置的资源,那么就应该:

public class HasUnmanaged : IDisposable
{
  IDisposable managedGoo;
  public void Dispose()
  {
    if(managedGoo != null)
      managedGoo.Dispose();
  }
}

这里没有加密的“处置”ool(怎么说可以称作>Dispose,而用false <>code>,用什么称作disating?) 在你们赢得的99.99%的 t需要时,没有任何人担心(第二种模式比第一个模式更为常见)。 所有这一切都是好的。

确实需要既管理有又管理有的资源? 无,你确实没有把未管理的资源总结在你自己的一类工作,然后处理上面第一种模式和第二大类。

只有在你因继承某类人而重新被迫执行“CA10634”模式时。 令人欣慰的是,多数人 t然制造了新东西。

永远不会忘记打电话<代码>Dispose(或,如你所说,更好地利用using)。

<分> 我猜测贵方案的目标是造成未经管理的资源泄漏。 然后可能是科索沃。

执行<代码>可支配<>代码>表明,一个类别使用未经管理的资源。 各位在相信你与这几类人重新接触时,应始终打电话到<条码>(<>条码>,尽可能使用<>条/条码>。 否则,你就不必要地保留未经管理的资源。

换言之,永远不会忘记打上<条码>。

是的,总是要求处置。 明示或默示(通过使用)。 例如,采用时间级。 如果你不明确停止行车,不进行处置,那么,在停车场收集员到车旁收集时,你会继续开枪。 这实际上会造成坠毁或意外的行为。

总是最好确保一旦与你接触,就立即提出反对。

微软(可能不是官方的)说,在某些情况下,它不叫放弃。

Stephen Toub from Morgan writes (大约称为任务处置):

In short, as is typically the case in .NET, dispose aggressively if it s easy and correct to do based on the structure of your code. If you start having to do strange gyrations in order to Dispose (or in the case of Tasks, use additional synchronization to ensure it s safe to dispose, since Dispose may only be used once a task has completed), it s likely better to rely on finalization to take care of things. In the end, it s best to measure, measure, measure to see if you actually have a problem before you go out of your way to make the code less sightly in order to implement clean-up functionality.

[bold emphasize is mine]

另一个例子是基流。

var inner = new FileStrem(...);
var outer = new StreamReader(inner, Encoding.GetEncoding(1252));
...
outer.Dispose();
inner.Dispose(); -- this will trigger a FxCop performance warning about calling Dispose twice.

(我拒绝了这一规则)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签