From 0fd8571ef6bcab1ad1e47bf51c927c2c0623e049 Mon Sep 17 00:00:00 2001 From: David Poeschl Date: Wed, 22 Apr 2015 15:28:42 -0700 Subject: [PATCH] Update EventMap.Registry.HasHandler to use .Equals This fixes internal TFS issue #1161343 The event handlers being tracked by EventMap may not be reference equal, so use .Equals to compare them instead of ==. This fixes an event handler leak and matches the equals behavior from before commit 7cda9431a8b24693e6e047a7dcef7e6047bfc151, when we used List.Remove. This change also adds some more careful null handling for when a Registry has a null handler. --- .../Core/Portable/Utilities/EventMap.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Workspaces/Core/Portable/Utilities/EventMap.cs b/src/Workspaces/Core/Portable/Utilities/EventMap.cs index 2431aa4c3c8..bf1ba4e3692 100644 --- a/src/Workspaces/Core/Portable/Utilities/EventMap.cs +++ b/src/Workspaces/Core/Portable/Utilities/EventMap.cs @@ -116,12 +116,27 @@ public void Invoke(Action invoker) public bool HasHandler(TEventHandler handler) { - return this.handler == handler; + return handler.Equals(this.handler); } public bool Equals(Registry other) { - return other != null && other.handler == this.handler; + if (other == null) + { + return false; + } + + if (other.handler == null && this.handler == null) + { + return true; + } + + if (other.handler == null || this.handler == null) + { + return false; + } + + return other.handler.Equals(this.handler); } public override bool Equals(object obj) @@ -131,7 +146,7 @@ public override bool Equals(object obj) public override int GetHashCode() { - return this.handler.GetHashCode(); + return this.handler == null ? 0 : this.handler.GetHashCode(); } } -- GitLab