Fix crash in C# JobSystem.Dispatch due to GC collecting Delegate object
This commit is contained in:
@@ -148,6 +148,20 @@ namespace FlaxEngine.Interop
|
|||||||
NativeMemory.AlignedFree(ptr);
|
NativeMemory.AlignedFree(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a delegate into a function pointer that is callable from unmanaged code via <see cref="Marshal.GetFunctionPointerForDelegate{TDelegate}"/> but cached <paramref name="d"/> delegate to prevent collecting it by GC.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDelegate">The type of delegate to convert.</typeparam>
|
||||||
|
/// <param name="d">The delegate to be passed to unmanaged code.</param>
|
||||||
|
/// <returns>A value that can be passed to unmanaged code, which, in turn, can use it to call the underlying managed delegate.</returns>
|
||||||
|
public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where TDelegate : notnull
|
||||||
|
{
|
||||||
|
// Example use-case: C# script runs actions via JobSystem.Dispatch which causes crash due to GC collecting Delegate object
|
||||||
|
ManagedHandle.Alloc(d, GCHandleType.Weak);
|
||||||
|
|
||||||
|
return Marshal.GetFunctionPointerForDelegate<TDelegate>(d);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts array of GC Handles from native runtime to managed array.
|
/// Converts array of GC Handles from native runtime to managed array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -464,7 +464,7 @@ namespace Flax.Build.Bindings
|
|||||||
return "FlaxEngine.Object.GetUnmanagedPtr({0})";
|
return "FlaxEngine.Object.GetUnmanagedPtr({0})";
|
||||||
case "Function":
|
case "Function":
|
||||||
// delegate
|
// delegate
|
||||||
return "Marshal.GetFunctionPointerForDelegate({0})";
|
return "NativeInterop.GetFunctionPointerForDelegate({0})";
|
||||||
default:
|
default:
|
||||||
var apiType = FindApiTypeInfo(buildData, typeInfo, caller);
|
var apiType = FindApiTypeInfo(buildData, typeInfo, caller);
|
||||||
if (apiType != null)
|
if (apiType != null)
|
||||||
|
|||||||
Reference in New Issue
Block a user