From 82f52b21eefa4ba7b07b4b31ae828cea08c872fa Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Mon, 4 Jan 2021 10:06:26 +0100 Subject: [PATCH] Fix NullReferenceException in StartReceive method (#39) --- src/Tmds.MDns/NetworkInterfaceHandler.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Tmds.MDns/NetworkInterfaceHandler.cs b/src/Tmds.MDns/NetworkInterfaceHandler.cs index 3403e88..58cbdc1 100644 --- a/src/Tmds.MDns/NetworkInterfaceHandler.cs +++ b/src/Tmds.MDns/NetworkInterfaceHandler.cs @@ -126,9 +126,14 @@ internal void Send(IList> packets) { try { + var socket = _socket; + if (socket == null) + { + return; + } foreach (ArraySegment segment in packets) { - _socket.SendTo(segment.Array, segment.Offset, segment.Count, SocketFlags.None, IPv4EndPoint); + socket.SendTo(segment.Array, segment.Offset, segment.Count, SocketFlags.None, IPv4EndPoint); } } catch @@ -194,7 +199,7 @@ private void OnReceive(IAsyncResult ar) lock (this) { #if NETSTANDARD1_3 - if (args.SocketError != SocketError.Success) + if (args.SocketError != SocketError.Success || _socket == null) { return; } @@ -239,7 +244,7 @@ private void OnReceive(IAsyncResult ar) } } } - } + } if (header.IsResponse && header.IsNoError) { for (int i = 0; i < header.QuestionCount; i++)