【发布时间】:2019-06-25 12:08:47
【问题描述】:
我调用该方法并得到 StackOverflowException。它不是递归调用,它只包含数组初始化。我需要一个 BigIntegers 数组,该代码适用于 int 数组,即使大小更大。我展示了简化的示例,在实际代码中我不能使用循环来填充数组,因为我无法生成我需要的数字,所以我必须对它们全部进行硬编码。
设置: x64 模式,.Net Core
从error details我们可以看出:
1) 堆栈跟踪为空
2) 错误可能源自 System.Collections.ListDictionaryInternal
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Before"); // <--- This is displayed
var a = GetBigIntegers(); // <--- Method is called
Console.WriteLine("After"); // <--- We will never get there
}
static BigInteger[] GetBigIntegers()
{
// <--- Crash here
return new BigInteger[]
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
// Many more lines (850-900) and they are 2-3 times longer than here
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
}
}
我检查了 IL 代码,它看起来是正确的,并且需要将近 400 000 行。
.method private hidebysig static
valuetype [System.Runtime.Numerics]System.Numerics.BigInteger[] GetBigIntegers () cil managed
{
// Method begins at RVA 0x207c
// Code size 1130123 (0x113e8b)
.maxstack 4
.locals init (
[0] valuetype [System.Runtime.Numerics]System.Numerics.BigInteger[]
)
// (no C# code)
IL_0000: nop
IL_0001: ldc.i4 66500
IL_0006: newarr [System.Runtime.Numerics]System.Numerics.BigInteger
IL_000b: dup
IL_000c: ldc.i4.0
// return new BigInteger[66500]IL_000d: ldc.i4.1
IL_000e: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
// (no C# code)
IL_0013: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
IL_0018: dup
IL_0019: ldc.i4.1
IL_001a: ldc.i4.1
IL_001b: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
IL_0020: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
.....
IL_113e75: dup
IL_113e76: ldc.i4 66499
IL_113e7b: ldc.i4.1
IL_113e7c: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
IL_113e81: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
IL_113e86: stloc.0
IL_113e87: br.s IL_113e89
IL_113e89: ldloc.0
IL_113e8a: ret
} // end of method Program::GetBigIntegers
我希望数组会被初始化并返回,但实际上我得到了 StackOverflow 错误。
我知道我可以使用不同的方法来做同样的事情,但我想知道为什么它不能以这种方式工作。希望每个阅读这个问题的人也能感兴趣。
【问题讨论】:
-
也发布堆栈跟踪怎么样?
-
删除其中几行会发生什么?删除这 900 行中的 90%,看看错误是否仍然发生。
-
我猜你超过了 40 亿大关?
-
检查您是在 x86 还是 x64 模式下运行项目。
标签: c# .net-core runtime-error runtime runtimeexception