I'm building an application which uses relatively large tables to do its work (LR tables, to be precise). As I'm generating code anyway and the table isn't that large, I decided to serialize my table by generating code that uses the C# collection initializer syntax to initialize the table on startup of my generated program:
public static readonly int[,] gotoTable = new int[,]
{
{
0,1,0,0,0,0,0,0,0,0,0,0,0,0,(...)
},
{
0,0,4,0,5,6,0,0,0,0,0,7,0,0,(...)
},
(...)
Oddly enough, when I generated a table that had only a couple hundred thousand entries, the application that I generated crashes with a StackOverflowException on startup. The C# compiler compiles it just fine; the table generation application also runs just fine. In fact, when I switched to Release mode, the application did start up. An OutOfMemoryException might have made some sense, but even then the table I use is way to small for an OutOfMemoryException.
Code to reproduce this:
Warning: trying the code below in release mode crashed Visual Studio 2010 for me; watch out for losing unsaved work. Additionally, if you generate code for which the compiler generates lots of errors, Visual Studio will hang as well.
//Generation Project, main.cs:
using (StreamWriter writer = new StreamWriter("../../../VictimProject/Tables.cs"))
{
writer.WriteLine("using System;");
writer.WriteLine("public static class Tables");
writer.WriteLine("{");
writer.WriteLine(" public static readonly Tuple<int>[] bigArray = new Tuple<int>[]");
writer.WriteLine(" {");
for (int i = 0; i < 300000; i++)
writer.WriteLine(" new Tuple<int>(" + i + "),");
writer.WriteLine(" };");
writer.WriteLine("}");
}
//Victim Project, main.cs:
for (int i = 0; i < 1234; i++)
{