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,684
  • 15
  • 54
  • 83

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
  • 129,300
  • 32
  • 216
  • 265
1

HTH

Community
  • 1
  • 1
Sunny
  • 6,286
  • 2
  • 25
  • 27
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
  • 1,026,079
  • 266
  • 2,566
  • 2,900