Bug SS-2294
3 votes

hard crash on recursive functions

Created by Davide on 2/16/2016 6:46 PM Last Updated by Davide on 4/24/2016 7:20 PM
%
 (hrs)
Logged: 0   (hrs)

 Description

I know these functions are bad design, still an overflow error messge would be better than an unrecoverable crash :X

    Mike Kaganski (Saturday, April 23, 2016 3:23 PM) #

Searching for a solution, there seems to be no good check against stack overflow, except checking StackTrace.FrameCount for some hard-coded value, that you may choose testing how deep a stack trace may be before crash. I only hope that won't apply big performance penalty.

 

Another good candidate (maybe the best) is RuntimeHelpers.EnsureSufficientExecutionStack Method - but it's only available since .NET 4. It allows to check for approaching stack overflow without imposing hard-coded limits that could become wrong in the future.

 

Given the importance of the problem, I would ask to use both ways: implement an function that would guard against "stack space available low" (e.g. by throwing exception) and use it before each user function's invocation; the guard function should depend on .NET framework version it runs on. If it's in 4.0+, then it will use RuntimeHelpers.EnsureSufficientExecutionStack Method, otherwise it would fallback to check if StackTrace.FrameCount is below static threshold.