8

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?

etoisarobot
  • 7,504
  • 15
  • 51
  • 82

3 Answers3

3

Take a look at FxCop for VS2010 - I believe they restored the DisposeObjectsBeforeLeavingScope rule - which may do exactly what you want.

LBushkin
  • 125,412
  • 32
  • 212
  • 261
1

HTH

Community
  • 1
  • 1
Sunny
  • 6,166
  • 2
  • 24
  • 26
0

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).

Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830