Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring call stack size in Visual Studio

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 facing stack overflow issue. I know something might be wrong about some recursive functions I am using, but before solving these issues I would like to monitor the call stack size to see what is going on.

like image 897
vanna Avatar asked Jul 26 '12 10:07

vanna


3 Answers

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.

like image 50
Hans Passant Avatar answered Oct 13 '22 19:10

Hans Passant


There are a few ways:

  1. Examine ESP in the watch window. You can do this by watching @esp in the watch window. Compare this to what ESP was at the start of the process.
  2. Similarly, examine the address of stack-allocated variables in first / last stack frames.

Note that the stack is usually allocated backwards, so as the stack grows, ESP gets smaller and smaller.

like image 24
tenfour Avatar answered Oct 13 '22 18:10

tenfour


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.

like image 43
Mark Lakata Avatar answered Oct 13 '22 19:10

Mark Lakata