Skip to content

Commit 4f41f8f

Browse files
committed
Refactor dispose-related code.
Use Dispose on all platforms. Extract and use a helper method.
1 parent f109f0c commit 4f41f8f

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -613,41 +613,30 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
613613
private void ShutdownSocket()
614614
{
615615
m_payloadHandler = null;
616-
if (m_networkStream != null)
617-
{
618-
#if NETSTANDARD1_3
619-
m_networkStream.Dispose();
620-
#else
621-
m_networkStream.Close();
622-
#endif
623-
m_networkStream = null;
624-
}
625-
if (m_tcpClient != null)
626-
{
627-
try
628-
{
629-
#if NETSTANDARD1_3
630-
m_tcpClient.Dispose();
631-
#else
632-
m_tcpClient.Close();
633-
#endif
634-
}
635-
catch (SocketException)
636-
{
637-
}
638-
m_tcpClient = null;
639-
m_socket = null;
640-
}
641-
else if (m_socket != null)
616+
Utility.Dispose(ref m_networkStream);
617+
SafeDispose(ref m_tcpClient);
618+
SafeDispose(ref m_socket);
619+
}
620+
621+
/// <summary>
622+
/// Disposes and sets <paramref name="disposable"/> to <c>null</c>, ignoring any
623+
/// <see cref="SocketException"/> that is thrown.
624+
/// </summary>
625+
/// <typeparam name="T">An <see cref="IDisposable"/> type.</typeparam>
626+
/// <param name="disposable">The object to dispose.</param>
627+
private static void SafeDispose<T>(ref T disposable)
628+
where T : class, IDisposable
629+
{
630+
if (disposable != null)
642631
{
643632
try
644633
{
645-
m_socket.Dispose();
646-
m_socket = null;
634+
disposable.Dispose();
647635
}
648636
catch (SocketException)
649637
{
650638
}
639+
disposable = null;
651640
}
652641
}
653642

@@ -676,7 +665,6 @@ private void SetFailed()
676665
m_state = State.Failed;
677666
}
678667

679-
680668
private void VerifyState(State state)
681669
{
682670
if (m_state != state)

0 commit comments

Comments
 (0)