7

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?

Johan
  • 74,508
  • 24
  • 191
  • 319
  • 2
    Look at the exception stacktrace. This should narrow down the possibilities and pinpoint the location. – Darin Dimitrov Aug 28 '11 at 17:55
  • 1
    thats total greek to me, the only thing i can find in any error log is application: crawler.exe Framework Version: v4.0.30319 Description: The process was terminated due to stack overflow. –  Aug 28 '11 at 17:57
  • 2
    +1 for asking a stackoverflow question on SO. – Johan Aug 28 '11 at 18:00
  • 1
    Well, then you could start narrowing down the problem by removing parts from your code until you find the offending piece. I hope you don't expect from this question someone answering you, on line 17 of Crawler.cs replace xxxx by yyyy. Another possibility is to show your code (or at least the part that you think runs when the exception occurs) so that other people might take a look at it. A more trained eye could spot it very quickly (if you are lucky). – Darin Dimitrov Aug 28 '11 at 18:02
  • no however, something along the lines of encasing the code in something that will actually catch the error would be great, because i have everything in a try{} catch{} but it wont catch because the application terminates, take out code until you find the offender is really not a good answer here considering i'm back to a version which had no problems at all. –  Aug 28 '11 at 18:04
  • And i dont know where the exception occurs. –  Aug 28 '11 at 18:07
  • @Mike, that's how I would proceed in the absence of magic crystal ball allowing me to immediately show me the errors in my code :-) One day when I have it I will know but until then work is needed ... – Darin Dimitrov Aug 28 '11 at 18:08
  • Well besides being cynical, maybe actually reading more than the title of the question, all your comments where nonconstructive, for example, i said i tried debugging and it doesn't occur, i tried restoring to an older version which did not have this issue. –  Aug 28 '11 at 18:12
  • I would add lots of tracing (log4net/NLog) to a log file and see where it stops. – Uwe Keim Aug 28 '11 at 18:23

2 Answers2

7

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.

Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • From my own experience, and what [MSDN](http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx) says, stack overflow exceptions are _not_ caught: "Starting with the .NET Framework version 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the `HandleProcessCorruptedStateExceptionsAttribute` attribute." – Uwe Keim Aug 28 '11 at 18:32
  • Wish i had a couple of those options, using an XP box. –  Aug 28 '11 at 19:00
  • @Mike: you can use 'adplus -crash -pn myprocess.exe' on XP. – Dmitry Aug 28 '11 at 19:02
  • i've been trying to use this http://www.symantec.com/business/support/index?page=content&id=TECH87208, but this wasnt generating a log –  Aug 28 '11 at 19:05
  • @Mike: adplus should work. Also try this: http://kb.acronis.com/content/2192. It may be a good idea to temporarily introduce artificial crash in your process, so that you can learn how to gather .dmp files. – Dmitry Aug 28 '11 at 19:13
  • i'm trying out adplus and it says it logs to to X directory, however there is no log there –  Aug 28 '11 at 19:18
  • @Mike: You are not looking for log. You are looking for .dmp file that will get generated after the process crashes. – Dmitry Aug 28 '11 at 19:19
  • actually i found it and it gives me back 1st chance UnknownException exception ----;. 2nd chance UnknownException exception ----;.echo ---------------------------------------------------------------;.echo;.echo Occurrence happened at: ;.tim –  Aug 28 '11 at 19:22
  • dmitry i finally figured out how to process everything in a log, i'm going to make a new post because it's all still greek to me. posted here http://stackoverflow.com/questions/7223785/c-stackoveflow-exception –  Aug 28 '11 at 20:44
  • The AppDomain.UnhandledException event is not fired for StackOverflowExceptions, even if you enable legacyCorruptedStateExceptionsPolicy or use HandleProcessCorruptedStateExceptionsAttribute. See here: dotnetslackers.com/articles/net/… – MikeWyatt 2 hours ago – MikeWyatt Apr 09 '12 at 19:11
3

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;
    }
}
Adam Maras
  • 26,269
  • 6
  • 65
  • 91
  • Man, I just lost 2 hours trying to find this issue - totally had an issue similar to what you described but instead of a property it was a method not properly calling the overload method:public static string[] GetSQLQueries(DataRow ReportRow). Shoot me in the face! { return GetSQLQueries(ReportRow); } – Chris Smith Dec 18 '13 at 21:38