views:

135

answers:

5

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?

+1  A: 

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.

Henrik
Half right yeah. It *is* being set within a lambda, but the type referred to in error message isn't a compiler generated class, it's the type for the enclosing method.
Dougc
I've updated the original question with a bit more context regarding where the code is.
Dougc
A: 

Are you sure that you aren't assigning a ManualResetEvent to a field of your class somewhere else?

Nicole Calinoiu
Yep. The `ManualResetEvent` is only used within the `using` block, which is within a private method.
Dougc
A: 

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.

TomTom
+1  A: 

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

Razzie
It's the version that comes with Visual Studio 2008 (version 9.0.30729.1 SP). As far as I know I've not installed FxCop seperately.
Dougc
+1  A: 

According to this post it's a known bug and therefore it should be save to ignore the error.

Simon Linder