Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Resharper be set to warn if IDisposable not handled correctly?

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 omit the proper Dispose call in a finally block?

like image 982
serg10 Avatar asked Sep 19 '08 13:09

serg10


3 Answers

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.

like image 170
Ilya Ryzhenkov Avatar answered Oct 14 '22 00:10

Ilya Ryzhenkov


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, inputs are welcome.

like image 6
kizzx2 Avatar answered Oct 14 '22 02:10

kizzx2


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.

like image 3
Jay Bazuzi Avatar answered Oct 14 '22 01:10

Jay Bazuzi