简体   繁体   中英

Is CA1001: TypesThatOwnDisposableFieldsShouldBeDisposable Valid?

If I have the following code:

public class Foo
{
    public void Bar()
    {
        var someTypeWithAnEvent = new SomeTypeWithAnEvent();        

        using (var signal = new ManualResetEvent(false))
        {
            someTypeWithAnEvent.Begun += (sender, e) => signal.Set();
            someTypeWithAnEvent.Begin();
            signal.WaitOne();
        }
    }
}

FxCop seems to throw a CA1001 error:

CA1001 : Microsoft.Design : Implement IDisposable on 'Foo' because it creates members of the following IDisposable types: 'ManualResetEvent'.

This doesn't seem valid in this instance because I'm disposing of the ManualResetEvent through the using block.

Am I missing something here or is there an error in the rule?

Seems like a false warning indeed. What version of FxCop are you using? It is reportedly a bug but might be solved now.

Let me guess: you're accessing signal in a lambda expression and the '..' in the error message is a compiler generated class. In this case it's safe to suppress the message.

根据这篇文章,这是一个已知的bug,因此应该保存以忽略错误。

I agree. This makes no sense - the signal will not survive undisposed. Looks to me like an error in the parser (for the condition). I would document it and put a pgragma into the file to supporess it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM