fix: NetworkClient.Disconnect avoids NullReferenceException if Transport ClientDisconnect calls OnDisconnected immediately. Related to: https://github.com/vis2k/Mirror/issues/2353

This commit is contained in:
vis2k 2020-10-21 20:40:14 +02:00
parent ebc9f7a4b3
commit 85be663b13

View File

@ -128,7 +128,14 @@ public static void Disconnect()
if (connection != null)
{
connection.Disconnect();
connection.Dispose();
// connection.Disconnect calls Transport.ClientDisconnect.
// if transport calls Mirror's OnDisconnected event immediately
// before returning, then we end up in a circular call:
// https://github.com/vis2k/Mirror/issues/2353
// in which case connection becomes null after returning here.
// in other words, the following Dispose call needs to use '.?'
// to avoid NullReferenceExceptions.
connection?.Dispose();
connection = null;
RemoveTransportHandlers();
}