Skip to content

Commit d3c80c9

Browse files
committed
Restore the original dictionary design to StrongComSafe and adapt the GetWrappers method instead
1 parent 9d288f0 commit d3c80c9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Rubberduck.VBEEditor/ComManagement/StrongComSafe.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
45

56
namespace Rubberduck.VBEditor.ComManagement
@@ -8,7 +9,7 @@ public class StrongComSafe: ComSafeBase
89
{
910
//We override the equality comparison and hash code because subclasses of SafeComWrapper<T> override the corresponding methods.
1011
//We need to distinguish between the individual wrapper instances no matter whether they are semantically equal.
11-
private readonly ConcurrentDictionary<int, ISafeComWrapper> _comWrapperCache = new ConcurrentDictionary<int, ISafeComWrapper>();
12+
private readonly ConcurrentDictionary<ISafeComWrapper, byte> _comWrapperCache = new ConcurrentDictionary<ISafeComWrapper, byte>(new ReferenceEqualityComparer());
1213

1314
public override void Add(ISafeComWrapper comWrapper)
1415
{
@@ -18,8 +19,8 @@ public override void Add(ISafeComWrapper comWrapper)
1819
Trace = GetStackTrace(3, 3);
1920
#endif
2021
_comWrapperCache.AddOrUpdate(
21-
GetComWrapperObjectHashCode(comWrapper),
22-
value => comWrapper,
22+
comWrapper,
23+
key => 1,
2324
(key, value) =>
2425
{
2526
#if DEBUG
@@ -32,8 +33,7 @@ public override void Add(ISafeComWrapper comWrapper)
3233

3334
public override bool TryRemove(ISafeComWrapper comWrapper)
3435
{
35-
return !_disposed && comWrapper != null &&
36-
_comWrapperCache.TryRemove(GetComWrapperObjectHashCode(comWrapper), out _);
36+
return !_disposed && comWrapper != null && _comWrapperCache.TryRemove(comWrapper, out _);
3737
}
3838

3939
private bool _disposed;
@@ -46,7 +46,7 @@ protected override void Dispose(bool disposing)
4646

4747
_disposed = true;
4848

49-
foreach (var comWrapper in _comWrapperCache.Values)
49+
foreach (var comWrapper in _comWrapperCache.Keys)
5050
{
5151
comWrapper.Dispose();
5252
}
@@ -57,7 +57,7 @@ protected override void Dispose(bool disposing)
5757
#if DEBUG
5858
protected override IDictionary<int, ISafeComWrapper> GetWrappers()
5959
{
60-
return _comWrapperCache;
60+
return _comWrapperCache.Keys.ToDictionary(GetComWrapperObjectHashCode);
6161
}
6262
#endif
6363
}

0 commit comments

Comments
 (0)