How to correct Stack UNDERflow error

北城以北 提交于 2020-01-07 08:33:08

问题


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.


回答1:


Ok, first off an Invalid Program Exception indicates something is wrong with the compiler, not your application. This means that you can test to see if the problem still exists (or has morphed into a different problem) without running your application. Once you see this error you are out of the normal debugging scenario -- you haven't done something wrong, and thus can't find or fix the problem by examing variables and seeing where you are doing the wrong thing.

The first suspect is probably optimizations--build without optimizations and see if your exe is still messed up. If so, you may consider turning optimizations off a sufficient workaround. You might even be able to use the System.Runtime.CompilerServices.Methodimpl(methodimploptions.nooptimization) to turn it off for a specific method.

If not, or that doesn't change anything, you want to find out what is triggering the bug, and a good staring point for that is Peverify. Run it against the compiled exe and it will list all of the places where bad code exists, even if it is an uncalled method.

Since this is a compiler bug, you can't step through a method to see exactly which line is off. That has it's upside -- you don't have to have a functioning (or as I said above, reachable) method. As long as the code compiles, it will either have the problem or not, so you can find the offending line(s) by commenting out or deleting lines in the method that is causing problems.



来源:https://stackoverflow.com/questions/35924569/how-to-correct-stack-underflow-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!