简体   繁体   English

检查实现IDisposable但未正确处置的对象

[英]Checking for Objects that implement IDisposable but aren't properly disposed

Is there a way to automatically check existing C# source code for instances of objects that are not properly disposed of ie. 有没有一种方法可以自动检查现有C#源代码中是否存在未正确处置的对象实例。 using try / catch / finally or using statements? 使用try / catch / finally或using语句? Or do I need to just manually look at the code? 还是我只需要手动查看代码?

看看的FxCop为VS2010 -我相信他们恢复了DisposeObjectsBeforeLeavingScope规则-它可以做的正是你想要的。

HTH 高温超导

CodeRush has some support for spotting obvious variants of this. CodeRush支持发现这种形式的明显变体。 Another possible option is (perhaps via an optional compilation symbol) add a finalizer to your own IDisposable objects, and complain loudly if they get finalized without being disposed (set a bool flag in the Dispose ). 另一个可能的选择是(也许通过一个可选的编译符号)向您自己的IDisposable对象添加一个终结器,并大声抱怨它们是否被终结而不被处置(在Dispose设置一个bool标志)。 But note: having a finalizer changes the behaviour of the objects, so don't leave it in production code (or even your regular unit tests). 但是请注意:拥有终结器会更改对象的行为,因此不要将其保留在生产代码(甚至常规的单元测试)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于搜索尚未处置的IDisposable对象的完整解决方案的工具 - Tools for Searching a complete solution for IDisposable objects that haven't been disposed 如何正确实现IDisposable - How to implement IDisposable properly 存储在集合中的IDisposable对象是否应该手动处理? - Should IDisposable objects stored in collections be disposed manually? 当明确处理所有成员时,类是否需要实现IDisposable? - Does a class need to implement IDisposable when all members are explicitly disposed? 如果程序意外关闭,IDisposable对象是否会被丢弃? - Do IDisposable objects get disposed of if the program is shut down unexpectedly? IDisisposable对象是否使用RequestContainer在Nancy请求的末尾处理? - Do IDisposable objects get disposed at the end of a Nancy request using the RequestContainer? 传递给IDisposable对象的引用的类是否也是IDisposable并在处理时将它们设置为null? - Should classes that are passed references to IDisposable objects be IDisposable as well and set them to null when disposed? 存储库类不会在ServiceStack中处置 - Repository classes aren't getting disposed in ServiceStack 处理所有实现IDisposable的嵌套对象 - Dispose of nested objects which all implement IDisposable NUnit是否处置实现IDisposable的对象? - Does NUnit dispose of objects that implement IDisposable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM