IDisposable-使用构造的自动检查 [英] IDisposable - automated check for using construct

查看:44
本文介绍了IDisposable-使用构造的自动检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道一种自动找到任何变量的方法吗,其中该类型实现IDisposable但未使用using构造?

Does anybody know of a way to automatically find any variable, where the type implements IDisposable but the using construct is not used?

即.一种检查潜在未释放的非托管资源的方法?

ie. a way to check for potentially unreleased unmanaged resources?

还可以查看正在运行的应用程序所拥有的资源的数量和类型吗?

Also, is it possible to see the number and types of resource held by a running application?

推荐答案

对此有一个代码分析规则:

There's a code analysis rule for this:

http://msdn.microsoft.com/zh-cn/library/ms182289%28VS.100%29.aspx

这可以从VS 2010 Premium或Ultimate运行,也可以与FxCop分别运行:

This can be run from VS 2010 Premium or Ultimate or separately with FxCop:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=917023f6-d5b7-41bb-bbc0-411a7d66cf3c

我见过的另一件事是在构造 IDisposable 对象时捕获堆栈跟踪,然后如果敲定键(意味着 Dispose()不是使用构造的堆栈跟踪记录错误.这很昂贵,因此您可能只想在开发中进行操作,或者仅在应用程序第二次遇到此问题时才开始收集堆栈跟踪信息(如果您一次遇到它,则很可能会在其中多次遇到它.一次应用执行).此方法适用于寿命更长的 IDisposable 实例(而不仅仅是局部变量).当然,它也仅适用于自定义 IDisposable 对象,因为它需要在构造函数/处置/定型器中使用自定义代码.

Another thing I've seen done is to capture a stack trace when an IDisposable object is constructed and then if the finalize is hit (meaning Dispose() was not called) log an error with the constructed stack trace. This is expensive so you may only want to do it in development, or only start collecting stack traces the second time your app runs into this problem (if you run into it once, you're most likely going to run into it many times within a single app execution). This method works for IDisposable instances that are longer lived (not just local variables). Of course it also only works for custom IDisposable objects since it requires custom code in the constructor/dispose/finalizer.

这篇关于IDisposable-使用构造的自动检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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