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

前端 未结 5 1336
醉话见心
醉话见心 2020-12-01 18:00

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

相关标签:
5条回答
  • 2020-12-01 18:15

    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.

    0 讨论(0)
  • 2020-12-01 18:31

    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.

    0 讨论(0)
  • 2020-12-01 18:32

    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.

    0 讨论(0)
  • 2020-12-01 18:33

    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.

    0 讨论(0)
  • 2020-12-01 18:33

    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.

    0 讨论(0)
提交回复
热议问题