37

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
serg10
  • 31,923
  • 16
  • 73
  • 94

5 Answers5

29

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.

Ilya Ryzhenkov
  • 11,782
  • 1
  • 40
  • 50
  • Not sure why you got voted down Ilya, makes sense for someone knowledgable in R# and the way it works to comment on a question about R# – Shaun Austin Sep 19 '08 at 14:34
  • I dunno, I've got voted down on another ReSharper related question, too. Looks like someone don't like me, personally. Or hate ReSharper :) – Ilya Ryzhenkov Sep 19 '08 at 14:37
  • 6
    Well I for one love it, and it's great to have people related to the product posting on StackOverflow. Cheers! – Shaun Austin Sep 19 '08 at 14:42
  • 4
    Certainly, it could only issue the warning if the variable was scoped to a single method without having to check if it was disposed somewhere else. – KyleMit Dec 26 '13 at 04:00
  • 1
    I understand your argument, Ilya. However, it seems like you could get a lot of "bang for your buck" by identifying specific, commonly-used methods that are known to produce `IDisposable`s that don't dispose themselves in their own methods. Failing to dispose an HttpResponseMessage can cause cryptic timeouts in other parts of code, and it's rare to intentionally use `await SendAsync()` without a `using` statement. In those rare cases, people can use Resharper's comments and such to indicate that they know what they're doing. – StriplingWarrior Sep 30 '15 at 21:50
6

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
kizzx2
  • 18,775
  • 14
  • 76
  • 83
4

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.

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
2

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.

user15749
  • 221
  • 1
  • 2
2

You might want to look at FXCop for this: http://msdn.microsoft.com/en-us/library/ms182328(VS.80).aspx

It's a pity R# doesn't handle it, even if just a warning for fields in your class and/or variables you create.

Pang
  • 9,564
  • 146
  • 81
  • 122
user154380
  • 31
  • 2