IS 389

Debugging Lab

This lab requires that you download the file named IS389DebuggingCSharp.zip.Unzip the file, and open the project file in Visual Studio 2012.The file is on my Web site on the Lecture Notes page.

In this lab, you will learn how to:

  • Set breakpoints for purposes of interrogating the values of variables and objects.
  • Use the Immediate window view expressions.
  • Trace statement execution
  • Set hit count breakpoints that will suspend execution based on a counter. Break every 100 iterations, for example.
  • Use the Watch window(s) to look at program data.
  • Look at the Call Stack to see the order in which procedures have been called.

For the first part of this lab, you will work through the same example again and again so as to diagnose and fix a simple problem. Detecting an input file with extra carriage returns at the end of the file.

Turning on Line Numbers

Personally, I like line number to appear along the left margin of the Code Editor.

  1. On the menu bar, click Tools, Options. Expand the Text Editor, All Languages, General folder as follows:
  2. Check Line Numbers.

  3. Click OK to close the dialog box.

Setting a First Breakpoint

Breakpoints temporarily suspend execution of a program. While suspended, you can look at the state of the application:

  • Where the program is executing
  • The values of variables and objects
  1. In the Code Editor, activate the form named frmMain.cs.
  2. Locate the following statement on line 26 and click in the left margin to set a breakpoint. (The breakpoint will be hit just before the statement containing the breakpoint executes). Thus, the second form will have been created but will not have been displayed.

  3. To view the Breakpoints window, click Debug, Windows, Breakpoints. The Code Editor and Breakpoints window should appear as shown in the following figure:
    Note that the Breakpoints window will appear, by default, docked along the bottom of the screen.

  4. Run the application by pressing F5 or clicking the StartDebuggingbutton on the toolbar.
  1. On the main form, click the button titled File Errors. The breakpoint will be hit and execution suspended as shown in the following figure:
  1. Execution of the program should remain suspended before you proceed.

Using the Immediate Window

In the Immediate window, you enter expressions, which are dynamically evaluated by the system. The following figure shows the Immediate window:

The first expression (?f) displays the controls on the form f. Note that the two controls appear as members of the form (btnReadFile, lstID, and lstName). The second expression (?f.Text) shows the value of the form’s Text property.

  1. The application should still be running and execution suspended in the following statement in frmMain.cs.

  2. Click Debug, Windows, Immediate to view the Immediate window.

Enter the following expressions and note the results:
?f
?f.Text

?f.lstID.Items.Count
The last statement looks at the Count property of the Items collection of the ListBox named lstID on the form f.

  1. End the program by clicking the Stop Debugging button
  2. Clear all of the breakpoints by clicking Debug, Delete All Breakpoints.
  3. In the confirmation dialog box that appear, click Yes to delete all of the breakpoints.

Using the Immediate Window (Example 2)

Next, you will see how debug errors resulting from file input. The debugging session shows the errors resulting from extra carriage returns at the end of a sequential file, missing carriage returns. The following figures show the two files:

  1. Activate the Code Editor for the form named frmFileErrors.cs. Set a breakpoint on the while loop appearing on line 49.

  2. Run the program.
  3. Click the button titled File Errors.
  4. Click the Read File button.
  5. In the Open dialog box, select the file named IS389DebuggingCSharp\TextFileExtraTerminalCRs.txt. Click OK to close the dialog box. The breakpoint you set is hit and execution is suspended.
  6. Click Debug, Windows, Immediate to view the Immediate window, if necessary.
  7. Enter the following expressions:
    ?Line
    ?Fields
    ?dlgResult

  8. Continue to run the program by pressing F5 until the following exception is thrown?

  9. Return to the Immediate window while the above breakpoint is set. Enter the following expression to see what is in the variable Line (the line you just read)

  10. Note that the line is empty. Thus the array named Fields has one element empty string. Thus, Fields[1] causes the IndexOutOfRangeException.
  11. End the program.
  12. Clear all of the breakpoint by clicking Debug, Delete All Breakpoints. Again, click Yes in the confirmation dialog box.

Tracing Statement Execution

In many cases, it is useful to execute code, statement by statement, to see the execution path that an application is taking. As you trace execution, you could use the Immediate window to look at variables. Watch windows provide an easy way to look at the values of the same variables and objects as you trace a program’s execution.

  1. Activate the Code Editor for the form named frmFileErrors.cs. Set a breakpoint on the while loop appearing on line 49.
  2. Run the program. Click the button titled File Errors and click the Read File button.
  3. In the Open dialog box, select the file named IS389DebuggingCSharp\TextFileExtraTerminalCRs.txt. Click OK to close the dialog box. The breakpoint you set is hit and execution is suspended.
  4. Click the StepInto button or press F11 to single step through the while loop. The statement highlighted in yellow is always the next statement to execute.
  5. Continue to execute the program until the exception is thrown. End the program.

Setting WatchPoints

WatchPoints are used to show the same information as the Immediate window. However, the watch expressions are persistent from one debugging session to the next.

  1. Run the program.
  2. Click the button titled File Errors.
  3. Click the Read File button.
  4. In the Open dialog box select the file named IS389DebuggingCSharp\TextFileExtraTerminalCRs.txt. Click OK to close the dialog box. The breakpoint you set is hit.
  5. Click Debug, Windows, Watch, Watch 1 to display the first Watch window.
  6. Enter the following Watch expressions (the values in column 1):

  7. Press F11 to single step through the statements. As you do, the values of the watched variables change.
    Pay attention to what is happening. The values of the variables Line and Fields are updated each time the line is split. When the line with the carriage return is reached, and the line has been split, Line and array are empty.

  8. Now think about the fix to the problem. We will fix in class and see the result. To fix the problem, uncomment the code on lines 54-58.

  9. End the program and clear all of the breakpoints.

Setting a Hit Count Breakpoint

Hit count breakpoints are useful for debugging loops. Errors in loops often occur when the loop is initialized or when the loop terminates. The following example show a common error that occurs in a for loop when the last element in the array is not examined. Again, a Watch window will be useful to watch the array get initialized.

  1. First, clear all of the breakpoints.
  2. In the Code Editor, activate the form named frmLoopingErrors.cs.
  1. Set a breakpoint on line 87 as shown in the following figure:

  2. In the Breakpoints window, right-click the breakpoint (the maroon circle in the left margin), and then click Hit Count to define the "Hit Count".
  3. In the Breakpoint Hit Count window, select break when the hit count is equal to. Enter the value 9 as shown in the following figure. This will cause execution to be suspended when the breakpoint is hit 9 times.

  4. Click OK to configure the breakpoint and close the dialog box. Note that a “+” appears inside the breakpoint indicating that this is a hit count breakpoint.

  5. Run the program.
  6. On the main form, click the button titled Looping Errors.
  1. On the looping error form, click the button titled Looping Error 2.
  2. Activate the Code Editor. Note that the value of the variable i is 8. To view the value, move the cursor over the variable i. The statement containing the breakpoint has executed 9 times (from 0 to 8).
  3. Create the following WatchPoint to view the variable named List. Click the plus sign to expand the array items as shown in the following figure:

  4. As you continue to execute the program, a subscript out of range exception will occur because the code tried to reference an array element that is greater than the number of elements in the array. The following change to the loop will correct the problem.

  1. Clear all of the breakpoints.
  2. Activate the form named frmLoopingErrors.cs and set a breakpoint on line 78 as follows:

  3. On the main form, click the button titled Looping Errors.
  4. Click the button titled Looping Error 1. The breakpoint will be hit.
  5. Expand the Watch window and look at the variable list. As you can see, the preceding for loop begins iterating at 0 instead of zero thereby not initializing the first element.

Overflow and Numeric Errors

Numeric overflow occurs when a number is too big (or to small) to fit in the allocated space.

How overflow is handled depends on the current compiler settings. In the following steps, you will look at the compiler settings that control how numeric overflow is handled.

  1. In the Solution Explorer, right click the Project named IS389DebuggingCSharp. Make sure to right-click the project and not the solution. In the context menu, click Properties.
  2. Click the Build tab. You should see the following screen:

  3. Click the Advanced button to view the following screen:
  4. Make sure that the check box Check for arithmetic overflow / underflow are checked.

  5. Now run the program. Click the Arithmetic Errors button. In the following form, enter the value 100000 as both operands and click the Overflow button.

  6. Because of the previous setting, there is no numeric overflow but clearly the value is not correct. In other words, without the overflow check you will get incorrect results.
  7. Now, to back and recheck to box for overflow and underflow checks, run the program again, and enter the same values again in the preceding figure. This time, an unhandled overflow exception occurs.