Search code examples
c#code-analysisca1001

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?


Solution

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