Prevent issues with ObjectEvalValueHandler when being accessed on multiple threads
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user