InvalidProgramException occurs when nullable types used for method return types


Example method that caused the problem:
    public virtual int? ReturnsNullable(int? data)
        return data.GetValueOrDefault();
IL errors:
[IL]: Error: [D:\CodePlex\DynaCache\DynaCache.Tests\bin\Debug\test.dll : CacheableNullableReturnTypeMethod::ReturnsNullable][offset 0x00000034][found address of value 'System.Nullable`1[System.Int32]'][expected address of ref ] Unexpected type on the stack.

Problem is caused because a reference to a value type is placed on the stack for the "out object result" parameter of the TryGetCachedObject - it seems that types like Int32 worked by luck, even though invalid IL was generated. Nullable<Int32> pushed things over the edge and caused the runtime error.

Fix is to have a new local of type object and always read the result into that, casting or unboxing as appropriate on the way of the out the method.
Closed Mar 30, 2013 at 5:08 PM by MikeGoatly


wrote Mar 30, 2013 at 5:08 PM

Resolved with changeset 72348: Fixed problem with value types being returned from cacheable methods

wrote May 16, 2013 at 3:35 AM