Prevent issues with ObjectEvalValueHandler when being accessed on multiple threads
This commit is contained in:
parent
8d16c1fb35
commit
1b32ab921b
|
@ -1,5 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Concurrent;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace PorygonSharp.EvalValues
|
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)
|
public static IntPtr GetObjectPtr(object o)
|
||||||
{
|
{
|
||||||
|
@ -31,13 +31,13 @@ namespace PorygonSharp.EvalValues
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
CreatedPointers.Remove(hash);
|
CreatedPointers.TryRemove(hash, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
|
var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
|
||||||
var ptr = GCHandle.ToIntPtr(handle);
|
var ptr = GCHandle.ToIntPtr(handle);
|
||||||
ptrObject = new PtrObject(ptr, new WeakReference<object>(o));
|
ptrObject = new PtrObject(ptr, new WeakReference<object>(o));
|
||||||
CreatedPointers.Add(o.GetHashCode(), ptrObject);
|
CreatedPointers.TryAdd(o.GetHashCode(), ptrObject);
|
||||||
return ptrObject.Pointer;
|
return ptrObject.Pointer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue