0

I get an InvalidProgramException when trying to execute a particular procedure in an application I am working on when it has been compiled with optimizations (Visual Studio 2015). I used PEVerify to see what it says about the problem, and it tells me "Method[offset 0x00000351] Stack underflow".

Obviously I can fix the problem by turning optimizations off, but that is a less than optimal solution, as is waiting for MS to fix whatever bug causes it in the next version.

What can I do to fix a stack underflow error? If I had to guess I'd say that it's probably related to the fact that this class is somewhere around 18k lines, but there's not a lot I can do about that...

Edit: Just to be clear, I don't expect an answer along the lines of "delete lines 6276 and 6277", what I am looking for is general strategies for troubleshooting this type of problem in .net. Something like the answers to this ActionScript question: How to debug a runtime stack underflow error?, except specific to .net. I am posting this so that the next person that has this type of problem will have a starting point for what they might try to work around this issue.

4

1 回答 1

2

好的,首先 Invalid Program Exception 表明编译器有问题,而不是您的应用程序有问题。这意味着您可以在不运行应用程序的情况下测试问题是否仍然存在(或已演变为不同的问题)。一旦你看到这个错误,你就脱离了正常的调试场景——你没有做错什么,因此无法通过检查变量并查看你在哪里做错事来找到或解决问题。

第一个嫌疑人可能是优化 - 构建没有优化,看看你的 exe 是否仍然一团糟。如果是这样,您可以考虑关闭优化,这是一个足够的解决方法。您甚至可以使用 System.Runtime.CompilerServices.Methodimpl(methodimploptions.nooptimization) 为特定方法关闭它。

如果没有,或者这不会改变任何东西,您想找出触发错误的原因,而Peverify就是一个很好的起点。对已编译的 exe 运行它,它会列出所有存在错误代码的地方,即使它是一个未调用的方法。

由于这是一个编译器错误,因此您无法通过一个方法来准确查看哪一行是关闭的。这有它的好处——你不必有一个有效的(或者我上面说的,可达的)方法。只要代码编译,它就会有问题或没有问题,因此您可以通过注释掉或删除导致问题的方法中的行来找到有问题的行。

于 2016-03-11T06:39:23.297 回答