Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for DisposeAsync when maybe null #201

Open
JohanLarsson opened this issue Feb 5, 2020 · 1 comment
Open

Fix for DisposeAsync when maybe null #201

JohanLarsson opened this issue Feb 5, 2020 · 1 comment
Labels

Comments

@JohanLarsson
Copy link
Collaborator

Before:

#nullable enable
namespace N
{
    using System;
    using System.IO;
    using System.Threading.Tasks;

    public class C : IAsyncDisposable
    {private readonly IAsyncDisposable? disposable = File.OpenRead(string.Empty);

        public ValueTask DisposeAsync()
        {
            return default(ValueTask);
        }
    }
}

Not sure how we should generate the null check.
Only offering fix when notnull for now. Nullable case is likely rare.

@jeremyVignelles
Copy link

Nullable case is likely rare.

I have a lot of them :)

I spawn a process in a StartAsync() method, but my process is null before reaching StartAsync(), which made me do this:

if(this._process !=null)
{
   await this._process.DisposeAsync();
}

It might not be thread-safe, but for my use case it's ok.

A safer way might be to use Interlocked.Exchange()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants