#849 – Using the Call Stack in Visual Studio to Navigate within Your Code

When in break mode within Visual Studio, you can view the call stack in the Call Stack window.

When you bring up the Call Stack window, there will be a yellow arrow pointing to the top of the call stack, indicating the location of the next statement that will execute when you resume execution.  The Locals window will show the values of local variables within the current method.

849-001

At this point, you can double-click on another method within the call stack.  When you do, the code editor will show code for that other method and the Locals window will show local variables for the same method.  They will have the values that existed at the point that the method at the top of the call stack was called.  Notice that a yellow arrow still indicates the execution point, but a green arrow now appears, showing the current method being examined.

849-002

Advertisement

#848 – Viewing the Call Stack in Visual Studio

The call stack keeps track of the currently executing method in your application, and from where that method was called.  You can use the debugger in Visual Studio to view the current call stack when you are in break mode (at a breakpoint or stepping through your code).

If the call stack is not already visible, click on the Debug menu, then Windows and Call Stack.  (Or press Ctrl+D, C).

848-001

In the example below, we’ve started the application and are located in our Main method, which was called by native code.

848-002

If we now step into the Dog.BarkYourAge method, the call stack shows this method on the top of the stack, with Main as the second entry.  Main is just below BarkYourAge in the stack, because it called BarkYourAge.

848-003

If BarkYourAge then calls DogUtil.GenerateBark and we step into that method, we see:

848-004

If we return from GenerateBark, we see:

848-005

#847 – How the Call Stack Works

A call stack is an indication of which method in your application is currently executing and how the application got there.  When a method is called, information about that method is pushed onto the call stack.  When the method returns to its caller, that information is popped back off the stack.  In this way, the top of the stack always refers to the current method.  And, traversing down the stack, we see the sequence of calls that got us to the current method.

For example, assume that when your application starts, a Main method is called.  Information about Main is pushed onto the stack.

847-001

Assume that Main then creates an instance of a Dog object and calls its BarkYourAge method.

847-002

If BarkYourAge then calls method DogUtil.GenerateBark, we get:

847-003

When control returns from GenerateBark, we continue in the BarkYourAge method and the stack becomes:

847-002

#846 – A Call Stack Keeps Track of Methods that Have Been Called

When your application is running, Windows uses a stack data type to manage its call stack.  A call stack is essentially just a record of the currently active methods.  At the top of the stack is the current method that is executing.  Below this method in the stack is the method that called the current method.  At the bottom of the stack is the first method that began executing when your application started.

For example, if you have a Main method that calls method Dog.BarkYourAge and this method in turn calls a method named DogUtil.GenerateBark, the call stack will appear as follows during executing of GenerateBark.  (For simplicity, we’re ignoring the .NET Framework methods that are called before invoking Main).

846-001

When a method is called, it is pushed onto the stack.  When it returns to its calling method, it is popped off of the stack.