Here is an example of a job that works as intended without Burst. No errors. But with Burst there is always a stack overflow.
Burst confuses different method overloads. It goes into recursion.
The result of this job with Burst:
System.StackOverflowException: The requested operation caused a stack overflow.
The result of this task without Burst:
No errors.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
namespace Test
{
public class BurstTest : MonoBehaviour
{
private void Start()
{
var job = new TestJob();
var handle = job.Schedule();
handle.Complete();
}
[BurstCompile(OptimizeFor = OptimizeFor.Performance)]
private unsafe struct TestJob : IJob
{
public void Execute()
{
using var s = new TestStruct<int>(50);
}
[StructLayout(LayoutKind.Sequential)]
private struct TestStruct<T> : IDisposable where T : unmanaged
{
[NativeDisableUnsafePtrRestriction] private readonly int* lengthPtr;
[NativeDisableUnsafePtrRestriction] private T* ptr;
public TestStruct(int count)
{
lengthPtr = AllocHelper.Alloc(count, Allocator.Temp);
ptr = AllocHelper.Alloc<T>(count, Allocator.Temp);
}
public void Dispose()
{
AllocHelper.Free(lengthPtr, Allocator.Temp);
AllocHelper.Free(ptr, Allocator.Temp);
}
}
private static class AllocHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* Alloc<T>(T value, Allocator allocator) where T : unmanaged
{
var ptr = Alloc<T>(1, allocator);
*ptr = value;
return ptr;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* Alloc<T>(int count, Allocator allocator) where T : unmanaged
{
return (T*) UnsafeUtility.Malloc(sizeof(T) * count, UnsafeUtility.AlignOf<T>(), allocator);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Free<T>(T* memory, Allocator allocator) where T : unmanaged
{
UnsafeUtility.Free(memory, allocator);
}
}
}
}
}
9501328–1338274–BurstTest.cs (2.41 KB)