Tracking down a stackoverflow error in an windows service

混江龙づ霸主 提交于 2020-01-03 09:06:05

问题


I made a small tweak to one of my windows services and than I ran it and got,

Description: The process was terminated due to stack overflow.

So i went back to an old version and ran it and i'm still getting the stackoverflow error.

Worst part is i've debug both and i do not get this error to reoccur. How/what is the best way to find what's causing the overflow for a windows service?


回答1:


You can subscribe to AppDomain.UnhandledException to log the exception. Another option is to see if the crash dump got generated when the service crashed and use WinDbg to track down the issue. You can also configure windows to generate these dumps. WinDbg comes with the script that can attach to the process and generate dump when this process crashes.

From this article:

... and finding a StackOverflowException isn't that hard. If you run !clrstack and find a callstack of 200+ lines you can be more or less certain that this is your problem.

UPDATE:

If you use .NET 4.0, you need to add legacyCorruptedStateExceptionsPolicy element to the service config file in order to log exception in AppDomain.UnhandledException.




回答2:


Without knowing more about the specific problem, I can say that a StackOverflowException (or the native equivalent thereof) is caused by unbounded recursion 99% of the time. Check your code; make sure any recursive cases you're aware of have a correct end case, and make sure you're not doing something silly like recursively calling an accessor or mutator when you mean to be accessing a field:

private int something;

public int Something
{
    get
    {
        return Something; // return something;
    }

    set
    {
        Something = value; // something = value;
    }
}


来源:https://stackoverflow.com/questions/7222751/tracking-down-a-stackoverflow-error-in-an-windows-service

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