Erroneous "Access to disposed closure" warning

Given the following code:

using (myobj)
{
     Method(delegate { myobj.AnotherMethod(); });
}

I get a ReSharper warning saying that there is "Access to disposed closure" but this seems perfectly valid.

2 comments
Comment actions Permalink

That may be valid.  But then, it might not be.  What if "Method(...)" is:

private void Method(Action action)
{
   _callbackForLater = action;

  //or what if it does this?
   Thread.Start( action);
}

I think the issue here is that without a lot more analysis (and I don't know how much effort thats worth), this warning will have a bunch of false positives and will get ignored completely.  I have already changed it's severity to hint, even though I like that it makes me think the access through.

0
Comment actions Permalink

What about this:

using (myobj)
{
     Method(myobj);
}

private void Method(MyObject myobj)
{
     // Use myobj in a thread...
}

That would be as bad as your example but no R# warning is generated...

Without knowing that the delegate is going to be used after it is disposed it seems like this warning is incorrect.  Like you say, it would be neat for R# to detect when that condition occurs but I think that would be too complex to accurately asses at compile time.

0

Please sign in to leave a comment.