将一个IDisposable的内存泄露,如果你不使用using语句? [英] Will an IDisposable memory leak if you don't use a using statement?

查看:269
本文介绍了将一个IDisposable的内存泄露,如果你不使用using语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个IDisposable的内存泄漏,如果你不使用using语句?结果
如果是这样,有人可以提供内存泄漏举例来说,如果它没有太多的代码?

Will an IDisposable memory leak if you don't use a using statement?
And if so, can someone provide a memory leak example if its not much code?

推荐答案

这将创建一个类型实现的IDisposable 的一个实例,其中A正确编写的程序不的专知的被遗弃时能够经过自身充分清理,的必须的保证的Dispose 被称为该实例之前放弃它。任何程序,它不叫的Dispose 上未明确知道被罚款没有它是一种类型的

A correctly-written program which creates an instance of a type that implements IDisposable, and which is not specifically known to be capable of adequately cleaning up after itself when abandoned, must ensure that Dispose is called on that instance before abandoning it. Any program which fails to call Dispose on a type which is not specifically known to be fine without it is broken.

虽然这将是很好,如果自动定稿会照顾一切,这是一个非常糟糕的清理机制。它提供不保证有关排序,线程上下文,及时性或确定性完成的(使用确定性清理的时候,人们可以合理确定清除失败的程序将不会出现正常完成;最后确定使用时,程序可能看起来甚至没有试图清理的对象)正常完成。

Although it would be nice if automatic finalization could take care of everything, it's a pretty crummy cleanup mechanism. It provides no guarantees with regard to sequencing, threading context, timeliness, or certainty of completion (when using deterministic cleanup, one can make reasonably certain that a program won't appear to complete normally if cleanup fails; when using finalization, a program may appear to complete normally without even attempting to clean up an object).

微软可能曾经打算每的IDisposable 类应该能够若弃后本身充分清理,但是这仅仅是不实际的。在许多情况下,一类试图若弃会增加复杂性的巨量之后本身进行清理,并会简单地把一个破碎的节目,将有,很容易显而易见的问题追查,成破程序,通常工作除非终结器线程相对的一些其他线程的时序导致事情有些意外和非再现的方式失败。

Microsoft may once have intended that every IDisposable class should be able to adequately clean up after itself if abandoned, but that is simply not practical. In many cases, for a class to attempt to clean up after itself if abandoned would add a massive amount of complexity, and would simply turn a broken program that would have obvious problems that are easy to track down, into a broken program that usually works except when the timing of the finalizer thread relative to some other thread causes things to fail in some unexpected and non-reproducible fashion.

有一些类型其中,尽管实施的IDisposable ,是无条件安全的抛弃,也有一些人可能在某些情况下,可以安全地抛弃。这是罚款的情况下放弃这些类型在那里处理起来会很困难(例如,由于引用是由各种线程操作的多个对象举行,也没有很好的办法,使任何特定的对象,就可以知道它时,它保持最后幸存参考),提供一个文件一个人的理由相信,这种行动是安全和适当的。这样的行为是不恰当的,但是,在情况下,一个人接受未知的血统的IDisposable 的对象。

There are some types which, despite implementing IDisposable, are unconditionally safe to abandon, and there are some others which may be safely abandoned in certain circumstances. It is fine to abandon such types in situations where disposing them would be difficult (e.g. because references are held by multiple objects that are manipulated by various threads, and there's no nice way by which any particular object can know it when it holds the last surviving reference), provided that one documents one's reasons for believing that such action is safe and appropriate. Such behavior is not appropriate, however, in cases where one has accepted IDisposable objects of unknown lineage.

这篇关于将一个IDisposable的内存泄露,如果你不使用using语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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