Prevent issues with ObjectEvalValueHandler when being accessed on multiple threads

This commit is contained in:
Deukhoofd 2019-09-12 22:16:53 +02:00
parent 8d16c1fb35
commit 1b32ab921b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 4 additions and 4 deletions

View File

@ -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<int, PtrObject> CreatedPointers = new Dictionary<int, PtrObject>();
private static readonly ConcurrentDictionary<int, PtrObject> CreatedPointers = new ConcurrentDictionary<int, PtrObject>();
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<object>(o));
CreatedPointers.Add(o.GetHashCode(), ptrObject);
CreatedPointers.TryAdd(o.GetHashCode(), ptrObject);
return ptrObject.Pointer;
}
}