DiscordClient::Dispose() throws NullReferenceException if no succesfull connection was established
Created by: schaap
Summary
A NullReferenceException
is thrown when a DiscordClient is destroyed before a succesfull conncection was established. The cause is _webSocketClient being dereferenced without checking whether it is null.
Steps to reproduce
- Run the following code:
// No, do not change the token. It's invalid on purpose.
DiscordClient? client = new DiscordClient(new DiscordConfiguration{Token = "NotValid"});
try {
await client.ConnectAsync();
} catch (Exception) {}
client.Dispose();
client = null;
Details
Error was enountered in the v4.0 nightly build NuGet packages (683) and the bug has been verified against the current source on Github.
Notes
Use of the C# 8.0 nullable feature might help in finding similar errors.