Skip to content

Commit

Permalink
Remove event handlers from SocketAsyncEventArgs in NetworkStream
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Jul 24, 2021
1 parent c5c4c80 commit 028985a
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions MailKit/Net/NetworkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ void AsyncOperationCompleted (object sender, SocketAsyncEventArgs args)
tcs.TrySetException (new SocketException ((int) args.SocketError));
}

void Cleanup ()
{
if (send != null) {
send.Completed -= AsyncOperationCompleted;
send.AcceptSocket = null;
send?.Dispose ();
send = null;
}

if (recv != null) {
recv.Completed -= AsyncOperationCompleted;
recv.AcceptSocket = null;
recv.Dispose ();
recv = null;
}
}

void Disconnect ()
{
try {
Expand All @@ -139,10 +156,7 @@ void Disconnect ()
return;
} finally {
connected = false;
send.Dispose ();
send = null;
recv.Dispose ();
recv = null;
Cleanup ();
}
}

Expand Down Expand Up @@ -278,11 +292,7 @@ protected override void Dispose (bool disposing)
ownsSocket = false;
Disconnect ();
} else {
send?.Dispose ();
send = null;

recv?.Dispose ();
recv = null;
Cleanup ();
}
}

Expand Down

0 comments on commit 028985a

Please sign in to comment.