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?
views:
96answers:
3Take 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.: http://stackoverflow.com/questions/1033334/is-there-a-list-of-common-object-that-implement-idisposable-for-the-using-stateme 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).