소스 검색

Avoid event-based memory leak when client is not disposing instance.

drieseng 9 년 전
부모
커밋
4659bb804a
1개의 변경된 파일17개의 추가작업 그리고 9개의 파일을 삭제
  1. 17 9
      src/Renci.SshNet/Channels/ClientChannel.cs

+ 17 - 9
src/Renci.SshNet/Channels/ClientChannel.cs

@@ -104,17 +104,25 @@ namespace Renci.SshNet.Channels
 
         protected override void Dispose(bool disposing)
         {
-            if (disposing)
-            {
-                var session = Session;
-                if (session != null)
-                {
-                    session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
-                    session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
-                }
-            }
+            UnsubscribeFromSessionEvents(Session);
 
             base.Dispose(disposing);
         }
+
+        /// <summary>
+        /// Unsubscribes the current <see cref="ClientChannel"/> from session events.
+        /// </summary>
+        /// <param name="session">The session.</param>
+        /// <remarks>
+        /// Does nothing when <paramref name="session"/> is <c>null</c>.
+        /// </remarks>
+        private void UnsubscribeFromSessionEvents(ISession session)
+        {
+            if (session == null)
+                return;
+
+            session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
+            session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
+        }
     }
 }