How does the well-known “Process is terminated due to StackOverflowException” screen appear?

前端 未结 1 1273
無奈伤痛
無奈伤痛 2021-01-11 23:55

A curious question:

How does the well-known \"Process is terminated due to StackOverflowException\" screen appear if the stack for the current process is full? Is it

相关标签:
1条回答
  • 2021-01-12 00:21

    This message is displayed by the CLR. You can see the code in the SSCLI20 distribution, clr/src/vm/eepolicy.cpp source code file:

    void DisplayStackOverflowException()
    {
        PrintToStdErrA("\n");
    
        PrintToStdErrA("Process is terminated due to StackOverflowException.\n");
    }
    

    Which in turn is called by the EEPolicy::HandleFatalStackOverflow() method. The only reason you can see it at all is because you are running a console mode app so output to stderr ends up on the console window. And you'll only see it if Windows Error Reporting (WER) hasn't itself terminated the app.

    There is no option to intercept this exception, the CLR cannot continue running managed code since there is too little stack space left to run any managed code safely. The line of code after the DisplayStackOverflowException() call is:

        TerminateProcess(GetCurrentProcess(), COR_E_STACKOVERFLOW);
    
    0 讨论(0)
提交回复
热议问题