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. using try / catch / finally or using statements? Or开发者_开发百科 do I need to just manually look at the code?
Take a look at FxCop for VS2010 - I believe they restored the DisposeObjectsBeforeLeavingScope rule - which may do exactly what you want.
Use FX Cop for a rule to check if IDisposable are placed in a proper Using block...
You can use reflector to go through the object with IDisposable
Ref.: Is there a list of common object that implement IDisposable for the using statement? for additional idea
HTH
CodeRush has some support for spotting obvious variants of this. 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
). 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).
精彩评论