1

On our project I have noticed that there are many places where we forget to close some connections. Now the question is if it's possible to make visual studio show a warning if we don't close/dispose it?

A correct example would be:

var app = new Application();

try {
//Some code
} finally {
app.Dispose();
}

So my idea is if it's possible to make a warning that triggers if an Application/connection is created and an dispose is missing.

Kristian Tang
  • 45
  • 1
  • 1
  • 8
  • You can't do a warning when you forgot to close something, but you can do a warining in some place. See [Microsoft Docs About C#'s #warning Perprocessor Directive](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-warning). – Chayim Friedman Aug 20 '18 at 14:41
  • A little fix - It's not true that **you can't**, **you can but you won't want** - you could develop an extension of VS that does this... But you wouldn't do this for one project. – Chayim Friedman Aug 20 '18 at 14:44
  • Yea should probably be said it's more of a solution, 5 years and still going so quite large. – Kristian Tang Aug 20 '18 at 14:48
  • Don't need to be a warning, just something that can either trace the connections or something like telling us "oh hey, you forgot to close this.. FIX IT!!!" – Kristian Tang Aug 20 '18 at 14:50
  • This article might be of interest to you : https://andrewlock.net/creating-a-roslyn-analyzer-in-visual-studio-2017/ – kipy Aug 20 '18 at 15:07
  • Enable Code Analysis maybe? I don't know how old and well maintained it is, but at least it tells you to dispose `IDisposable` objects – Biesi Aug 20 '18 at 15:11
  • @BiesiGrr I tried enabling the CA2000 but it does not trigger.. here is my test code: var app = new Application(); app.LogOn("test", "test"); SerialPort port = new SerialPort("COM4"); port.Open(); – Kristian Tang Aug 21 '18 at 10:01

2 Answers2

0

There is a built-in static analysis tool, simply called "Code Analysis" that does exactly what you have asked. You can enable this on each project that you want this behavior to apply. Once enabled, you will get a compilation warning or error (configurable) any time a resource is not properly disposed.

See the following:

How to enable automatic code analysis

CA2000: Dispose objects before losing scope

Screenshot of enabling CA2000 and setting it to "Error":

enter image description here

Edit: There are actually two different types of static code analysis available. The one I mentioned above is the older version, sometimes referred to as "FxCop"

The newer type of code analysis is called Roslyn Analyzers. See info here: https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview

Dan Ling
  • 2,965
  • 2
  • 29
  • 43
  • I tried enabling the CA2000 but it does not trigger.. here is my test code: var app = new Application(); app.LogOn("test", "test"); SerialPort port = new SerialPort("COM4"); port.Open(); – Kristian Tang Aug 21 '18 at 11:06
  • In addition to checking the check box, did you set the dropdown to "Error"? – Dan Ling Aug 22 '18 at 21:03
-1

I would recommend circumventing the issue altogether with a using statement:

using(var app = new Application)
{
    some code...
}

The using statement will automatically dispose of your object (or connection) after execution, thus avoiding your issue of forgetting to close connections. That may have sidestepped your question a bit, but from the way it was worded this seemed to be the underlying issue.

More info on using statements

JWalli
  • 9
  • 2
  • Yea, my example was also just one use case, the problem is that I don't want to go though so much code to make sure that everything is closed, so I want something that tells me "hey this isn't closed" so that I can easy resolve the issue. another thing is that I have made it clear to my co-workers that they need to remember to dispose/close, because it is not acceptable. – Kristian Tang Aug 20 '18 at 15:04
  • Understandable, seems like it might be impractical to rework the code base this far in. Still, some coding standards for the organization might not be a bad suggestion for you to make (style cop, resharper, etc). Here's hoping for a more direct answer, sorry I couldn't help. – JWalli Aug 20 '18 at 15:33
  • What question is this answering? – Kenneth K. Aug 20 '18 at 17:58