Once your application development is underway, you will reach a point where your application builds and runs, but runtime errors occur. A good way to examine these errors is with Visual Studio’s built-in debugging tools. Using Visual Studio’s debugger, you can watch variables, examine the stack, insert breakpoints, and pause execution. The debugging functions enable you to interactively change variable values or call different functions, to methodically examine runtime issues.
Creating a Debug Build
To start debugging your application in Visual Studio, you’ll first need to create a debug build:
Working with Breakpoints
Once you have a debug build, you’ll want to identify places where you want to halt the application during execution and examine it for issues. This is accomplished by inserting breakpoints, or places where you want the debugger to stop execution and wait for further commands. For example, if your application encounters a runtime error when manipulating an array of data and outputting it to an XML file, you can insert breakpoints during the processing loop and examine the data or the processing code for errors.
To insert and use breakpoints:
Tip: To see all of your breakpoints, click Alt + F9.
Your application will deploy and start running, until it reaches your breakpoint. The figure below shows Visual Studio stopped at a breakpoint, which was set on the line of code that outputs “Hello World”. The code is highlighted in yellow, and the breakpoint is shown in the left margin as an arrow in an orange circle.
The Watch Window
The watch window enables you to look at values of variables in scope. You can also change the value of a variable for debugging purposes.
To use the watch window:
By default, the watch menu opens in a window in the lower left corner of Visual Studio. although you can undock and move the window elsewhere.
Tip: You can also show the value of a variable after you reach a breakpoint by hovering the mouse pointer over a variable in your code; its value will be shown in a tooltip.
The Call Stack
The call stack window enables you to see what functions have been called prior to getting to the current state. The call stack displays information only – you cannot edit or change any values. By default, the call stack opens in a window in the lower right corner of Visual Studio, although you can undock and move the window elsewhere.
To open the call stack, select Debug > Windows > Call Stack or click Alt + 7.
The Immediate Window
The immediate window allows you to alter values or even call functions. It is effectively a partial language interpreter.
To open the Immediate Window, select Debug > Windows > Immediate or click Ctrl + Alt + I.
Exiting the Debugger
To stop debugging, select Debug > Stop Debugging or click Shift+F5.
Other Debugging Tools
More debugging tools are available by selecting Debug > Windows. For example, windows are available to examine local variables, threads, processes, and memory.
Tip: You can also access many of the debugging tools from a Visual Studio toolbar. Right-click the toolbar area and select Debug from the list. The toolbar provides buttons to start, pause, and stop debugging, plus step into, over, and out of code, and show breakpoints.
For More Information
For more information on debugging, read “Debugging in Visual Studio” on the MSDN at http://msdn.microsoft.com/en-us/library/sc65sadd.aspx.