Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

280 Project 1 DATABASE PROGRAMMING USING VC++.NET

remove them, rebuild the solution. Observe that the entries are no longer listed in the Task List window.

 

 

 

 

 

 

 

 

NOTE

 

 

 

 

 

 

 

 

Try out the other options of the Task List feature on your own.

 

 

 

 

 

You are now aware of one of the new and significant features of Visual Studio

 

 

 

 

 

Y

 

 

.NET. Next, you will learn about breakpoints and how to work with the Break-

 

 

points window.

 

L

 

 

 

 

F

Breakpoints Window

M

 

 

 

 

 

 

 

 

Typically, a program runs many pages of code, and to manually trace a bug in such

 

 

 

 

A

 

 

 

lengthy code not only is an extremely tedious task but also is virtually impossible.

 

 

 

E

 

Tools, such as breakpoints,Twere introduced as an aid to debugging. If you have worked with Visual C++ 6.0 (in Visual Studio 6.0), you are aware of breakpoints. A breakpoint enables you to instruct the debugger to suspend a program temporarily during execution. The suspended sate of the program is referred to as break mode. In simple words, you can correlate break mode to the “pause” feature of a stereo. Just as you can resume to hear the song from where it was paused, you can resume the execution of the program from the point it entered break mode. The similarity ends there!

When the program is in break mode, you can examine the code. For instance, you can check the value of a variable at that point in time or the state of the methods invoked in the program. You can also change the order of execution of statements in the code. In Visual C++, you can edit the code in break mode and continue to execute the program with the changes. This is possible because of the Edit and Continue feature.

You do not need to code to insert breakpoints; instead, you can just click on the gray margin next to the line where you want the breakpoint. Alternatively, you can use the New Breakpoint dialog box to set the breakpoint. To invoke this dialog box, you can either choose Debug, Breakpoint or click on the New option in the Breakpoints window (see Figure 9-8).

Team-Fly®

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

281

 

 

 

 

 

FIGURE 9-8 The Breakpoints window

Before examining the Breakpoints window, you should first get acquainted with the New Breakpoint dialog box and learn to use its different options.

The New Breakpoint Dialog Box

You can use the different tabs of the New Breakpoint dialog box, shown in Figure 9-9, to customize the breakpoint that you set.

FIGURE 9-9 The New Breakpoint dialog box

282 Project 1 DATABASE PROGRAMMING USING VC++.NET

The dialog box includes four tabs, described here:

Function. Use this tab to specify the name of the function in which you want to set the breakpoint. In addition, by using the Line and Character text boxes, you can specify the line number and character where you want to set the breakpoint.

File. Use this tab to specify the name of the file within which you want to set the breakpoint. The values (file name, line number, and character number) in this tab appear automatically when you either specify a function name in the Function tab or click on the gray area next to the line where you want to set the breakpoint. When you specify a function name in the Function tab, the line number and the character number under the File tab are set to the same values as in the Function tab. When you click on the gray area next to the line where you want to set the breakpoint, the file name that contains the line, line number, and character number are determined automatically and the values are set.

Address. Use this tab to specify the memory address where you want to suspend the application. Notice that the Line and Character text boxes are not available under this tab.

CAUTION

The values of the memory address must be specified either in decimal or hexadecimal and must be integer constants only.

Data. Use this tab to monitor the variable based on whose value you will suspend your application. In other words, changes in the value of the specified variable will activate the breakpoint and suspend the application.

CAUTION

Data breakpoints can be set for native C/C++ code only.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

283

 

 

 

 

 

The four tabs of the New Breakpoint dialog box share two properties: Condition and Hit Count, which have corresponding buttons in the dialog box. These two properties enable you to further customize the behavior of the breakpoint.

Condition. You can also attach a condition with a breakpoint. This allows you to instruct the debugger to enter break mode only when the value of the specified expression(s) changes or evaluates to true. Suppose you are debugging an airline application. You need to check the reservation module of the application; in other words, whether the module reports a message, “seats unavailable,” when the number of seats available for a flight reaches zero. To do this, you might set a breakpoint in the reservation module and attach a condition no_of_seats < 0, where

no_of_seats is the variable storing the number of available seats for a flight. When you execute this application, if you encounter a transaction that returns the value of no_of_seats as zero, then the program will enter break mode. To specify a condition, click on the Condition button of the New Breakpoint dialog box. In the Breakpoint Condition dialog box that appears, shown in Figure 9-10, you can specify the desired condition.

FIGURE 9-10 The Breakpoint Condition dialog box

Hit Count. By default, the debugger causes the program to enter break mode each time it encounters a breakpoint. However, you can change the number of times the breakpoint is to be ignored during the execution of the application and when it should be executed. To do so, you use the

Hit Count property of the breakpoint. The Hit Count property specifies the number of times the breakpoint is hit before the application enters the break mode. To set the hit count, click on the Hit Count button of

284 Project 1 DATABASE PROGRAMMING USING VC++.NET

the New Breakpoint dialog box. The Breakpoint Hit Count dialog box, shown in Figure 9-11, appears.

FIGURE 9-11 The Breakpoint Hit Count dialog box

The following are the different values that you can assign to the Hit Count property of a breakpoint, which are listed in the “When the breakpoint is hit” drop-down list in the Breakpoint Hit Count dialog box:

break always

break when the hit count is equal to <n>

break when the hit count is a multiple of <n>

break when the hit count is greater than or equal to <n>

In the preceding values, <n> represents the number of times the breakpoint is to be hit.

TIP

Typically, you assign a hit count value to a breakpoint to trace bugs that are not expected to occur the first time you run your application. One of the classic examples where you might need to specify a hit count is when you are working with loops.

Any breakpoint that you set in your application (either by clicking on the gray area next to the line or by using the New Breakpoint dialog box) appears in the Breakpoints window (shown previously in Figure 9-8). In the following sections, you will learn to set breakpoints in your application and also examine the options available in the Breakpoints window.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

285

 

 

 

 

 

Inserting a Breakpoint

Now, you will learn to set a breakpoint using both methods discussed earlier. To demonstrate the use of breakpoints, in the DebugApp application, a menu option is used, as shown in Figure 9-12.

FIGURE 9-12 The menu designed for the DebugApp application

Add an event handler for the menu option, MyMenu1, to the view class, and add the following code to the handler:

void CDialogAppView::OnMenuMymenu1()

{

// TODO: Add your command handler code here int i;

for(i=0; i<20; i++) AfxMessageBox(i);

}

Build the project, execute the application, and choose Menu, MyMenu1. A debug assertion failure occurs and even if you ignore it, you notice that the problem reoccurs. You choose to use breakpoints to debug this application. However, before you insert a breakpoint, you will have to end this application. To accomplish this,

286 Project 1 DATABASE PROGRAMMING USING VC++.NET

click on the Abort button (if the Debug Assertion dialog box is active); otherwise, switch to the application, and choose Debug, Stop Debugging. To add a breakpoint:

1.Click on the gray area next to the for loop. A new breakpoint, represented by a dark-brown bullet, appears next to the line of code.

2.Choose Debug, Windows, Breakpoints to view the Breakpoints window. The new breakpoint appears in the window, as shown in Figure 9-13.

FIGURE 9-13 The Breakpoints window with a breakpoint entry

3.Build and execute the application. Once you choose Menu, MyMenu1, the application enters break mode. Figure 9-14 depicts the application in break mode.

FIGURE 9-14 The application in break mode

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

287

 

 

 

 

 

In addition to the Breakpoints window, other debugging windows are activated: Autos, Locals, Watch1, Call Stack, and Command. You will learn about these windows in the subsequent sections.

Notice that the Debug toolbar is also added to the application window. The toolbar has various options that you can use to perform various tasks, such as to stop the debugging and continue executing the application.

4.On the Debug toolbar, click on the Continue button, the first button on the toolbar. Alternatively, you can press F5 to continue execution. A message box appears in the application, but it doesn’t have any value displayed in it. However, as per the code, it should display the value of the variable i.

When you click on the OK button, the Debug Assertion Failed dialog box appears again. Click on the Retry button to enter again into break mode. Another message box pops up indicating that the debugger has encountered an unhandled exception.

When you click on the Break button, the insertion point is positioned next to the AfxMessageBox method. Recall that the AfxMessageBox method accepts a string as the parameter and not an integer variable.

The Breakpoints window has a toolbar that includes various tools, described in Table 9-2. The tools are described in the order they appear in the toolbar. Use the tool tips to identify the names of the tools.

Table 9-2 Breakpoints Window Toolbar

Tools

Used to

New

Insert a new breakpoint. When you click on this option, the New

 

Breakpoint dialog box appears.

Delete

Delete a specific breakpoint.

Clear All Breakpoints

Remove all breakpoints in the current file.

Disable All Breakpoints

Disable all breakpoints in the current file. As the name specifies,

 

the breakpoints are only disabled and not deleted.

Go To Source Code

Transfer the control to the line in the code corresponding to the

 

breakpoint.

continues

288

Project 1

DATABASE PROGRAMMING USING VC++.NET

 

 

 

 

 

 

 

 

Table 9-2 (continued)

 

 

 

 

 

 

 

 

 

Tools

 

Used to

 

 

 

 

 

 

 

Go To Disassembly

Transfer the control to the line in the assembly code generated by

 

 

 

 

 

the compiler corresponding to the line that has the breakpoint set.

 

 

Columns

Add more columns to the Breakpoints window, such as Language,

 

 

 

 

 

File, Address, Hit Count, and so on. These columns display the

 

 

 

 

 

values that you set using the New Breakpoint dialog box.

 

 

Properties

Invoke the Breakpoints Properties dialog box that lists the prop-

 

 

 

 

 

erties of the selected breakpoint. This dialog box is similar to the

 

 

 

 

 

New Breakpoint dialog box.

 

 

 

 

 

 

The following is the second method that you can use to set a breakpoint. Before trying this method, you need to remove the previously set breakpoint. To do so, click on the Delete button in the Breakpoints window.

1.Position the insertion point in the line of code where you want to set the breakpoint. In the Breakpoints window, click on the New button. Alternatively, you can either choose Debug, New Breakpoint or press Ctrl+B. The New Breakpoint dialog box appears (refer to Figure 9-9).

2.In the Function text box, type OnMenuMymenu1. This is the function where you want to set the breakpoint.

3.Accept the default values in the Line and Character text boxes, and click on the OK button. A breakpoint is added next to the line and also appears in the Breakpoints window.

NOTE

If there are functions in the same name but in different classes, the IntelliSense feature displays a list of those functions, from which you can select the desired function.

4.In the Breakpoints window, click on the Properties button to open the Breakpoints Properties dialog box, shown in Figure 9-15.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

289

 

 

 

 

 

FIGURE 9-15 The Breakpoints Properties dialog box

The function name that you specified is displayed.

5.Click on the File tab. The name of the file containing the breakpoint is displayed by default.

6.Click on Cancel to close the dialog box, and remove the breakpoint.

TIP

When you right-click on a breakpoint symbol, a number of options are displayed. Most of these options are the same as those in the Breakpoints window. Other, additional options are available, such as Add Task List Shortcut, which enables you to add a shortcut to the breakpoint in the Task List.

Autos Window

The Autos window displays the variable names and their values involved in the currently executed statement and those involved in the previously executed statement. As the name of the window indicates, the debugger adds the values to this