VS warning: Disposable object created by 'disposable' is never disposed, why?

ε祈祈猫儿з 提交于 2020-01-04 05:09:21

问题


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?

来源:https://stackoverflow.com/questions/57926135/vs-warning-disposable-object-created-by-disposable-is-never-disposed-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!