浏览代码

Unsubscribe from events before disposing WaitHandles.
This reduces the likelyhood of invoking Set() on a disposed WaitHandle.
Incomplete fix for issues #1944 and #1942 as eventhandlers could still be invoked after unsubscription.

Gert Driesen 11 年之前
父节点
当前提交
0a8ac33d46
共有 1 个文件被更改,包括 12 次插入12 次删除
  1. 12 12
      Renci.SshClient/Renci.SshNet/Channels/Channel.cs

+ 12 - 12
Renci.SshClient/Renci.SshNet/Channels/Channel.cs

@@ -712,6 +712,18 @@ namespace Renci.SshNet.Channels
                 {
                     this.Close(false);
 
+                    //  Ensure that all events are detached from current instance
+                    this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
+                    this._session.ChannelDataReceived -= OnChannelData;
+                    this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
+                    this._session.ChannelEofReceived -= OnChannelEof;
+                    this._session.ChannelCloseReceived -= OnChannelClose;
+                    this._session.ChannelRequestReceived -= OnChannelRequest;
+                    this._session.ChannelSuccessReceived -= OnChannelSuccess;
+                    this._session.ChannelFailureReceived -= OnChannelFailure;
+                    this._session.ErrorOccured -= Session_ErrorOccured;
+                    this._session.Disconnected -= Session_Disconnected;
+
                     // Dispose managed resources.
                     if (this._channelClosedWaitHandle != null)
                     {
@@ -735,18 +747,6 @@ namespace Renci.SshNet.Channels
                     }
                 }
 
-                //  Ensure that all events are detached from current instance
-                this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
-                this._session.ChannelDataReceived -= OnChannelData;
-                this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
-                this._session.ChannelEofReceived -= OnChannelEof;
-                this._session.ChannelCloseReceived -= OnChannelClose;
-                this._session.ChannelRequestReceived -= OnChannelRequest;
-                this._session.ChannelSuccessReceived -= OnChannelSuccess;
-                this._session.ChannelFailureReceived -= OnChannelFailure;
-                this._session.ErrorOccured -= Session_ErrorOccured;
-                this._session.Disconnected -= Session_Disconnected;
-
                 // Note disposing has been done.
                 this._isDisposed = true;
             }