1

I'm developing an application on 'VisualStudio 2017', 'C# 6.0', 'R#'. In C# 6.0 was introduced new feature - support async/await' withintry/cath'. i use using operator (which is really try/finaly under the hood), but i still receive warning Access to disposed closure. But really msGet shouldn't not be disposed before DownloadToStreamAsync.

  • Does it can really access disposed variable?
  • Is it R# glitch?

my code:

using( var msGet = new MemoryStream() )
{
    var stopwatchAp = Stopwatch.StartNew();
    var stopwatchPureDownload = new Stopwatch();
    var times = new long[ 201 ];
    var retryCount = -1;
    await this._ap.Do( () =>
    {
        stopwatchPureDownload.Restart();
        var downloadToStreamAsync = blob.DownloadToStreamAsync( msGet );
        downloadToStreamAsync.ContinueWith( t => times[ ++retryCount ] = stopwatchPureDownload.ElapsedMilliseconds );
        return downloadToStreamAsync;
    } ).ConfigureAwait( false );
}

Update 1 I have seen similar questions before:

Access to disposed closure in C#? (but example here doesn't contains await, it is not about 'async/await')

Calling asynchronous method in using statement . (the answer says - useawait. But as you can see my code contains await- but i still have the warning )

Also these questions dated to 2013 year. There was no C# 6.0 in 2013 year. That's why I'm asking.

18
  • 3
    Please post the code not the image, this is a bad practice Commented Apr 13, 2017 at 10:55
  • 2
    @Alex it was possible to await inside try block, but was not possible to await inside catch or finally blocks (prior to C# 6). So that is not really relevant to this question. Commented Apr 13, 2017 at 10:58
  • 3
    As for the question - Resharper is just not smart enough to recognize captured msSet will not be disposed yet when async block runs. It just sees that you captured and use some variable which is later disposed (not related to async at all). Commented Apr 13, 2017 at 11:02
  • 2
    The date of the original posts is irrelevant. The situation is the same. The language doesn't introduce breaking changes like this Commented Apr 13, 2017 at 11:12
  • 3
    Besides - what does Do do? Who says that it executes the lambda before returning? There is no guarantee that msGet will be available when the code actually executes. Do may be storing it for later execution. Commented Apr 13, 2017 at 11:14

1 Answer 1

2

Your Do method takes a Func<Task>, inside the invocation where you provide this, you include reference to msGet, this is a closure, and the compiler will turn this into a field that is accessed from your Func<Task>.

Resharper cannot be sure that your usage of the Func<Task> is within the scope or the lifetime of the using block, you could for example inside the Do method, copy the func into a field and call it again later.

If you are executing the Func<Task> immediately inside of Do you can safely ignore the warning with:

// ReSharper disable once AccessToDisposedClosure
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you =) Glad I could help. Feel free to mark this answer as accepted if it has resolved your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.