Fix a memory leak in the loonix waterbox interop code that's never been used before ever

This commit is contained in:
nattthebear 2017-07-12 17:57:29 -04:00
parent 9cb44cad03
commit 2bd7726b9b
1 changed files with 27 additions and 4 deletions

View File

@ -155,6 +155,16 @@ namespace BizHawk.Common.BizInvoke
throw new InvalidOperationException("Out of Thunk memory"); throw new InvalidOperationException("Out of Thunk memory");
} }
private int FindUsedIndex(object o)
{
for (int i = 0; i < _refs.Length; i++)
{
if (_refs[i] == o)
return i;
}
return -1;
}
private static void VerifyParameter(Type type) private static void VerifyParameter(Type type)
{ {
if (type == typeof(float) || type == typeof(double)) if (type == typeof(float) || type == typeof(double))
@ -201,10 +211,23 @@ namespace BizHawk.Common.BizInvoke
} }
public IntPtr GetFunctionPointerForDelegate(Delegate d) public IntPtr GetFunctionPointerForDelegate(Delegate d)
{
// for this call only, the expectation is that it can be called multiple times
// on the same delegate and not leak extra memory, so the result has to be cached
lock (_sync)
{
var index = FindUsedIndex(d);
if (index != -1)
{
return GetThunkAddress(index);
}
else
{ {
return GetArrivalFunctionPointer( return GetArrivalFunctionPointer(
Marshal.GetFunctionPointerForDelegate(d), new ParameterInfo(d.GetType()), d); Marshal.GetFunctionPointerForDelegate(d), new ParameterInfo(d.GetType()), d);
} }
}
}
public IntPtr GetArrivalFunctionPointer(IntPtr p, ParameterInfo pp, object lifetime) public IntPtr GetArrivalFunctionPointer(IntPtr p, ParameterInfo pp, object lifetime)
{ {