4

I am writing a C# application and have stumbled across something that makes me unsure if it is a Visual Studio bug, or my knowledge is incorrect.

The culprit is a using statement inside "catch" part of a try/catch, where Visual Studio 2019 shows a warning that a disposable object will not be disposed:

Disposable object created by 'MakeMeSomeDisposableObject()'s is never disposed

The same statement used outside the catch, shows no warning message.

Here is an example of what I am talking about:

try
{
    SomeMethodThrowingExceptions();
}
catch
{
    using(var someDisposableObject = MakeMeSomeDisposableObjects())
    {
        someDisposableObject.SaveTheWorldFromException();
    }
}

My understanding is that in case of an exception - the disposable object will save the world and get disposed of after finishing its job because of the using statement. Visual Studio however thinks otherwise and shows a warning message, but also shows no fix - is this a VS bug or am I wrong about how using statements work in a catch?

Rachey
  • 211
  • 2
  • 9
  • "Disposable object created by 'MakeMeSomeDisposableObject()'s is never disposed". It's the warning hint when you press ctrl + . – Rachey Sep 13 '19 at 15:12
  • @TimSchmelter The part about using inside the catch is relevant though - other usages of this method with a "using" statement don't show this warning, which makes me unsure if it's not just an IDE bug. – Rachey Sep 13 '19 at 15:15
  • 2
    and just to be sure, if you put the `using` block inside the try, or outside of the try catch all together, then the error message goes away? – Casey Crookston Sep 13 '19 at 15:16
  • @CaseyCrookston Exactly. – Rachey Sep 13 '19 at 15:17
  • What version of Visual Studio? – David Browne - Microsoft Sep 13 '19 at 15:17
  • VS 2019 Professional, 16.2.4 – Rachey Sep 13 '19 at 15:18
  • 1
    I also don't get it in VS 2017 (15.9.9). – Casey Crookston Sep 13 '19 at 15:23
  • 2
    Code analysis warnings about IDisposable usage are notoriously unreliable, it does skirt having to solve the halting problem. There is no history of getting this specific usage wrong, maybe it doesn't actually look like that? Anyhoo, just move on, [disable the warning](https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019#suppress-violations) and let Microsoft know about it with Help > Send Feedback > Report a Problem. – Hans Passant Sep 13 '19 at 15:30
  • Checked the exact code on a borrowed machine from a colleague - nothing shows up on older VS version. I will just report it to Microsoft and call it a day. – Rachey Sep 13 '19 at 15:33
  • Maybe its because the compiler expands a using block into an actual `try/catch` block; its hiding `MakeMeSomeDisposableObject` now. What happens if you declare `someDisposableObject` outside and then add a `finally` block and dispose of it there? – Trevor Sep 13 '19 at 15:43
  • What I am trying to explain is that the `catch` block can actually throw an error *if* that using statement could fail, so the compiler is telling you its possible that `MakeMeSomeDisposableObjects` doesn't get disposed inside that catch because the using statement creates it's own try/catch and hence it's only a warning... TBH you shouldn't do this, do it inside the try and or declare it outside... – Trevor Sep 13 '19 at 15:50
  • Can you try this `using var someDisposableObject = MakeMeSomeDisposableObjects; someDisposableObject.SaveTheWorldFromException();` does it still give you the warning? – Trevor Sep 13 '19 at 15:59
  • 2
    I will, but it's the "Friday afternoon when things are burning and it's a release day" time of the year, so it will land on a to-do list. – Rachey Sep 13 '19 at 16:01

0 Answers0