From 1b32ab921b0509eaf9a6777816727ae9cc561e01 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 12 Sep 2019 22:16:53 +0200 Subject: [PATCH] Prevent issues with ObjectEvalValueHandler when being accessed on multiple threads --- PorygonSharp/EvalValues/ObjectEvalValueHandler.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PorygonSharp/EvalValues/ObjectEvalValueHandler.cs b/PorygonSharp/EvalValues/ObjectEvalValueHandler.cs index 1e5f359..2ea8682 100644 --- a/PorygonSharp/EvalValues/ObjectEvalValueHandler.cs +++ b/PorygonSharp/EvalValues/ObjectEvalValueHandler.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Collections.Concurrent; using System.Runtime.InteropServices; namespace PorygonSharp.EvalValues @@ -18,7 +18,7 @@ namespace PorygonSharp.EvalValues } } - private static readonly Dictionary CreatedPointers = new Dictionary(); + private static readonly ConcurrentDictionary CreatedPointers = new ConcurrentDictionary(); public static IntPtr GetObjectPtr(object o) { @@ -31,13 +31,13 @@ namespace PorygonSharp.EvalValues } else { - CreatedPointers.Remove(hash); + CreatedPointers.TryRemove(hash, out _); } } var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection); var ptr = GCHandle.ToIntPtr(handle); ptrObject = new PtrObject(ptr, new WeakReference(o)); - CreatedPointers.Add(o.GetHashCode(), ptrObject); + CreatedPointers.TryAdd(o.GetHashCode(), ptrObject); return ptrObject.Pointer; } }