I am experiencing an InvalidProgramException, and can't quite seem to figure out the cause. The documentation says this is probably a bug either in the C# compiler or the JIT compiler. Running PEVerify yields a StackUnderflow error at the line I can trace the problem to, so I assume the problem is not in the JIT portion. But before I bring this to MSs attention, I want to make sure I'm not doing something retarded.
The following file reproduces the error consistantly:
namespace ErrorTest
{
class Program
{
public Program(int _) { i = _; }
public Program Begin() {
return new Program(0);
}
public static Program operator ++(Program b) {
Program tmp = new Program(b.i);
tmp.i++;
return tmp;
}
public int i;
static void Main(string[] args) {
Program x = new Program(0);
x.Begin()++;
//++x.Begin(); //This also fails
}
}
}
Realistically, x.begin()++; could be replaced with (new Program(0))++; and the results would be the same. I'm guessing the problem is that the temporary Program object is getting lost somewhere.
The operator++ implementation is what I'm mostly concerned with. Is that the typical methodology [construct, modify, and return a temporary]? It produces the sort of results one would expect, and I've found a few snippets scattered around the internet that would support it, but I want to make sure.
Unfortunately, I'm finding it rather hard to dissect the MSIL myself, but if it helps here's what I get out of the dissassembler:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 14 (0xe)
.maxstack 2
.locals init ([0] class ErrorTest.Program x)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: newobj instance void ErrorTest.Program::.ctor(int32)
IL_0007: stloc.0
IL_0008: call class ErrorTest.Program ErrorTest.Program::op_Increment(class ErrorTest.Program)
IL_000d: ret
} // end of method Program::Main
PEVerify says the stack underflow occurs at offet 0x8, which I assume refers to IL_0008 in that code.
If anybody could verify that I'm not doing anything obviously wrong, I'd appreciate it.
CM