Is there a setting in Resharper 4 (or even Visual Studio itself...) that forces a warning if I forget to wrap code in a using
block, or ommit the proper Dispose
See this blog post for some tricks for testing for Dispose()
in DEBUG
. Basically, write a DEBUG-only destructor that asserts that you were disposed.
You might want to look at FXCop for this: http://msdn.microsoft.com/en-us/library/ms182328(VS.80).aspx
Its a pity R# doesn't handle it, even if just a warning for fields in your class and/or variables you create.
Discontent with current methods, I created my own: EyeDisposable. It's an IL instrumenter so it should catch many leaks not caught by static analysis. It's still in its early stage, inpuits are welcome.
Correct automatic Dispose analysis requires DFA (Data Flow Analysis) in a global way. It is unlikely that you create an IDisposable object and doesn't call any method on it and do not pass it around as an argument. If disposable object is passed to other methods (including calling its members, when "this" is implicitly passed), the tool should analyse if Dispose is not called within, or that object is not stored somewhere for later disposal.
That said, naive implementation of checking if disposable object is in fact disposed with "using" construct or in any other way would yield too much false positives, and render analysis useless.
You could design a small add-in to R# that you could have run inside the code editor that scans the code and updates the code analysis to reflect that you an object who's missing the structure you've just described.
I'd look into the R# plugin architecture if you decide to go that route.