IDisposable的,真的很要紧 [英] IDisposable, does it really matter

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

问题描述

即将从C / C ++很久以前我还是有确保所有的资源都正确清理的习惯。我始终确保调用Dispose上了IDisposable类和落实含有处置的对象我的类的Dispose模式。

Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and implement Dispose patterns in my classes containing disposable objects.

不过,在我的环境我或多或少是唯一一个这样做。其他人就是不明白我在做什么,并认为我的code是更难理解。

However, in my environment I'm more or less the only one doing this. Others just don't understand what I'm doing and think my code is more difficult to understand.

他们刚刚创建的数据库连接,打开的流等,而无需调用Close或Dispose。有时,他们设置一个局部或成员变量设置为无在方法结束(猜他们的背景)。

They just create database connections, open streams etc without calling Close or Dispose. Sometimes they set a local or member variable to "Nothing" at the end of a method (guess their background).

我的问题是,他们的code工作一样好我的。 code随着时间的推移创建数千个数据库连接对象只是工作。

My problem is that their code works just as well as mine. Code that over time creates thousands of database connection objects just works.

所以,忽略了约code正确性任何参数,下面的指南等,不IDiposable真正的问题

So, ignoring any arguments about code correctness, following guidelines etc, does IDiposable really matter?

有没有人居然跑出资源从没有处置的对象?

Has anyone actually ran out of resources from not Disposing objects?

编辑: 感谢所有的答复。有趣的是,有些人有问题时,没有处置。这似乎虽然是罕见的,我假设GC / JIT确实保持资源使用的一个好工作下来在正常情况下。

Thanks for all replies. Interesting to see that some people have had problems when not Disposing. It seems to be rare though and I assume the GC/JIT does a good job of keeping resource usage down under normal conditions.

无论我collegues我也不会改变,因为这种行为,但它的感觉很好是正确的。

Neither my collegues nor I will change behavior because of this but it feels good to be right.

推荐答案

是的,我已经刷爆了甲骨文的游标数循环时,通过连接对象,例如,因为我忘了关闭命令阅读器 - 这是100只在单个连接上环过了,我需要支持与可能数以百计的连接做在同一时间。

Yes, I've maxed out the number of Oracle cursors when looping over a connection object, for example, because I forgot to close the command reader - that was only 100 loops on a single connection too, and I needed to support that with possibly hundreds of connections doing it at the same time.

您的开发伙伴们应学会使用 使用(){...} 语法 如果他们不能不屑关闭所有非托管资源本身。这是很好的做法反正,你应该使用它太,因为你自己可能会忘记把你的的Dispose()要求在终于{} 子句这样才能真正清理未处理的异常的情况下被抛出。

Your fellow developers should be taught to use the using() { ... } syntax if they can't be bothered to close up any unmanaged resources themselves. It is good practice anyways and you should use it too, since you yourself might be forgetting to put your Dispose() calls in a finally {} clause so as to truly clean up in the event of an unhandled exception being thrown.

如果你不能赢得他们的心 - 改变自己的想法 - 创造打破他们的code。通过麻杏指出,他们没有清理资源测试 - 然后显示,修复操作简单,易于,并允许其code是更具扩展性。或者只是展示给你的老板,并告诉他们,这将使他/她出售的产品作为一个新的版本,内置的:)你的开发伙伴们更多的可扩展性将指示做这一切的时候,在未来,希望,和你会在更高的方面太多举行。

If you can't win their hearts - change their minds - create tests that break their code by maxing out the resources that they're not cleaning up - and then show that the "fix" is simple and easy, and enables their code to be far more scalable. Or just show it to your boss and tell them this will enable him/her to sell the product as a new version with more scalability built in :) Your fellow developers will be instructed to do this all the time in the future, hopefully, and you'll be held in higher regard too.

这篇关于IDisposable的,真的很要紧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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