I have experienced Access Violation Exceptions and Fatal Execution Engine Error (it seems to be the same error) in managed code in C#. I have narrowed it down to the following snippet. Am I missing something? I thought it should not be possible to these kind of exceptions in managed code. I have seen it both in .net 4.7 and 4.5 and on multiple different computers? Is this a known issue in .Net?
public static class FatalExecutionEngineBugExposer
{
public static void Main(string[] args)
{
for (int i = 0; i < 500000; i++)
{
try
{
var testProblem = new TestProblem();
testProblem.AddTrianglesExperiment();
}
catch (Exception e)
{
Console.WriteLine(e.GetBaseException());
}
}
}
public class TestProblem
{
public struct Point
{
public double X, Y, Z;
}
public void AddTrianglesExperiment()
{
var points = new Point[28800];
Parallel.ForEach(Partitioner.Create(0, points.Length),
range =>
{
// Create cache
var cache = new CubeRefCache();
cache.Entries = new CubeRefCacheEntry[20000];
// Add triangles
for (int i = range.Item1; i < range.Item2; i++)
{
ProcessTriangle(ref points[i].X, ref points[i].Y, ref points[i].Z, cache);
}
});
}
void ProcessTriangle(ref double pt1, ref double pt2, ref double pt3, CubeRefCache cache)
{
cache.Add(0, 0);
}
public struct CubeRefCacheEntry
{
public int Index;
public int Item;
}
class CubeRefCache
{
internal CubeRefCacheEntry[] Entries;
internal void Add(int index, int item)
{
Entries[0].Index = index;
Entries[0].Item = item;
}
}
}
}
This snippet "typically" fails within a minute.
UPDATE: It should probably be run in 64-bit. If compiled with 'Any CPU', the issue takes a lot longer to reproduce.
UPDATE2: usings I have: