bookmark
Home > Windows Mobile > Knowledge Base > Developing

Developing : Debugging with Visual Studio 2008

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:

  1. Open your solution in Visual Studio.
  2. On the Build menu, click Configuration Manager.
  3. In the Active solution configuration list, click Debug. Click Close.
  4. On the Build menu, click Build Solution. This will build a debug version of the solution.

 

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:

  1. In your source code, find the lines where you’d like to place or set breakpoints.
  2. Add a breakpoint by doing one of the following:
    • Click F9.
    • Click in the grey strip in the left margin next to the line of code.
    • Right-click the line of code, and select Breakpoint > Insert Breakpoint.

      Tip: To see all of your breakpoints, click Alt + F9.

  3. To start debugging, click Debug > Start Debugging. (You can also click F5.)

    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.

  4. Once stopped at a breakpoint, you can step into the code, step over it, or step out of it.
    • Stepping into code refers to following down into a function call. To step into code, select Debug > Step Into, or click F11.
    • Stepping over code refers to not following down into a function call; it will still be made, just not take user down those function details. To step over code, select  Debug > Step Over, or click F10.
    • Stepping out of some code refers to getting out of a function that was stepped into. Once taken out of the code, execution will, when continued, go to the next line of code. To step out of code, select Debug > Step Out, or click Shift + F11.
  5. To continue after stopping at a breakpoint, select Debug > Continue, or click F5.
  6. When a breakpoint is no longer needed, you can delete them with any of the same methods you use to add a breakpoint.

 

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:

  1. On the Debug menu, select Windows > Watch > Watch1. (You can also click Alt + 3.)

    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.

  2. To change the value of a variable, enter a new value next to the variable name. You can also double-click a variable in your code and drag it to the watch window.

    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.

Questions or comments about this article? Talk about it at our Discussion Boards!

View Related Products

list
Highlights
viewProduct Showcase

Samsung Mobile Innovator

close
View Related Products
preview
nextview

Samsung Mobile Innovator

close