Tracking down a stackoverflow error in an windows service
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 b开发者_StackOverflow社区oth 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?
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.
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;
}
}
精彩评论