Is there a way to monitor the call stack size in Visual Studio ? A call stack window is provided while running but does not show the size of the stack. I am using C++ and fa
The "Microsoft Recommended Native Rules" Code Analysis can look at your code and find problems with your code that might overflow your stack. I'm not sure how good it is at finding a recursion problem, but it did find an issue in my code where I used a local instance of a class that was very big (1MB). At runtime, the only error was a stack overflow. It's a bad idea to use large objects on the stack of course; you should only use small objects and objects that store most of their dirty laundry on the heap.
In VS2012, right-click on the project Properties, and select Code Analysis, then click the checkbox to Enable Code Analysis. It takes a few minutes to run.
Using a data breakpoint can be helpful here. Wherever you happen to be in the code, it doesn't matter as long as you are on the right thread, use Debug + New Breakpoint + New Data Breakpoint. In the address box type @esp - 250000
. Press F5 to continue running and it will break somewhere inside the recursion when a quarter of the available stack space has been consumed. The exact offset from esp isn't critical.
There are a few ways:
@esp
in the watch window. Compare this to what ESP was at the start of the process.Note that the stack is usually allocated backwards, so as the stack grows, ESP gets smaller and smaller.