Debugging of the Delphi Application (iii) monitoring variables

Source: Internet
Author: User

Monitoring variables (watching Variables)

What does the user do when the program stops at a breakpoint? Usually the user stops at the breakpoint to check the value of the variable, is the value of a variable the same as the expected value? Or what value a variable takes (not knowing the value of the variable beforehand).

The Watch List window is useful for allowing the user to check the value of a variable. Programmers often overlook this simple yet fundamental feature because they do not take the time to fully learn the use of debuggers. Users can add any number of variables to watch list. Is the Watch List window during the debugging session.

Variable names and variable values are displayed in the watch list.

ToolTip expression Evaluation (ToolTip expressions Evaluation)

The debugger and code Editor have a good feature that makes it easier to check the value of a variable. This feature is the ToolTip expression evaluation (expression evaluator), whose default state is open, so the user does not need to do anything to use it. If necessary, you can also close it by using the Code Insight page of the Editor Properties dialog box, such as:

So, what is a ToolTip expression evaluated? It works like this: When the program stops at a breakpoint and the user moves the edit cursor over a variable, a prompt window pops up showing the current value of the variable. This makes it easier to quickly check the variables, just move the cursor over the variable and wait a moment.

The Tooltip evaluator is displayed differently for different variable types. For regular data members (Integer, Char, Byte, String, and so on), the actual value of the variable is displayed, and for dynamically created objects (such as instances of a class), the Tooltip evaluator displays all the elements of the record. As shown in the following:

Note

Sometimes the ToolTip evaluator seems to be working abnormally. For example, if you move the edit cursor over an extraterritorial variable, the prompt window is not displayed because the ToolTip evaluator has no information to display. Variables that are optimized by the editor may not display the correct values.

The Tooltip evaluator also does not work when the cursor is in the With block. For example, look at the following code:

1234567891011 var    pp:point; begin    with  pp do    begin      x: =;      y: = +;    end;     caption: = IntToStr (pp.x); end;

If you place the cursor on the variable x, the Tooltip evaluator does not display the value of x because x belongs to the WITH statement (point variable). If you move the cursor over the variable point, the debugger displays the value of the pointer (including the X field). Such as:

The TOOLTIP expression evaluation is useful, and don't forget to use it.

watch list shortcut menus (the Watch list context menu)

As with every Delphi window mentioned earlier, Watch list has its own shortcut menu. Described below:

edit watch-- to open the Watch Properties dialog box to modify the Watch property
Add watch-- to open the Watch Properties dialog box new Watch
Enable watch-- enables a monitored item that has been disabled
Disable watch-- Disabling an enabled watch item
Delete watch-- deletes a watch item
Enable all watches-- enabling all monitored items that have been disabled
Disable All watches-- Disable all monitored items that are enabled
Delete all watches-- Remove all monitoring items
Add group-- add a monitoring group
Delete group-- deletes a monitoring group
Move Watch to group-- Mobile watch to Group
stay on top-- makes the Watch List window always on top

inspect-- Displays information about the selected watch items
When changed-- is changed in the Watch window, the debugger will terminal. The watch variable is displayed in red, indicating that break when changed is working.
The dockable-- window is available for berth.

Use the Watch Properties dialog box (using the Watch properties Dialog box)

When a user adds or edits a watch, the Watch Properties dialog box is used. As shown is the Watch Properties dialog box that is editing the PP variable.

The expression field at the top of the dialog box is where the user enters the variable name to be edited or added to the watch list, which is a combo box that you can use to select previously used watches.

However, when the user wants to check the array, use the "Repeat count field", for example, there is an array of 20 integer elements, to check the first 10 integers of the array, you can enter an array in the Expression field (for example, Array[0]), and then in the Repeat Count field, enter 10. The first 10 elements of the array are displayed in the watch list. For example, pp is an integer array with 20 elements:

Note

If the array name is added to the watch list, all elements of the array are displayed in the watch list. When you want to view some elements of an array, use the Repeat count field.

Use the "digits field" only when checking floating-point numbers, where you enter the number of significant digits to display when the float is displayed, and the remainder after the effective bit is rounded. The Enabled field determines whether the monitoring item is currently enabled.

The rest of the options on the Watch Properties dialog box are display options. Each data type has a default display type, and when the user selects "Default", the default display type is used, and the default view option is "checked". Select other viewing options to view the data in other ways. Such as:

To modify a watch, click the item in the Watch list and select the Edit Watch ... menu item in the Right-click shortcut menu, or double-click the watch to quickly open the Watch Properties dialog box.

enable and disable monitoring items (enabling and disabling watch items)

As with breakpoints, each item in the Watch list can be enabled or disabled. When a watch is disabled, it becomes dimmed, and its value is displayed as <disabled>. Such as:

To disable a watch, click the name of the item in the Watch list and choose the Disabled Watch menu item from the Watch list shortcut menu, and to enable the watch, select the Enabled Watch menu item from the shortcut menu.

Note

Users may want to disable monitoring items that are not currently in use but will be used later. Many of the effective watches in watch list slow down program execution during debugging because all watch list variables are updated each time a line of code executes.

add a variable to watch list (Adding Variables to the watch list)

There are several ways to add a watch to watch list. The quickest way is to click on the variable name in Code Editor and choose Debug | From the shortcut menu in Code Editor. Add watch at Cursor "or press" Ctrl + F5 "key, the watch will be added to watch list immediately. If needed, the user can edit the watch and modify its properties.

To add a variable to the watch list without having to find the variable in the source file, select Run | from the main menu. Add Watch menu item, when the Watch Properties dialog pops up, enter the variable name to add to the watch list and click OK.

Note

Although you can add class instance variables to the watch list, the displayed values may be useless. Such as:

You should use Debug inspector to view all data members of the class.

use watch list (using the watch list)

When a breakpoint is encountered, watch list displays the current value of all of the variables, and if the Watch List window is not currently open, from the main menu "View | Debug Window | Watches "menu item opens the Watch List window.

Note

Dock the Watch List window to the bottom of the Code Editor window so that you can always see it when debugging your code.

In some cases, a message is displayed after the variable in Watch list, not the value of the variable. For example, if a variable is not outside the scope or is not found, the watch list displays "Undeclared identifier: ' X ' after the variable name, or if the program is not running or is constantly at a breakpoint, watch list is displayed after all the watches [ Process not accessible]. Display <disabled> when monitoring items are disabled. Other messages may also be displayed depending on the current state of the application or the current state of a variable.

As mentioned in the previous chapter, users occasionally see this display in watch list: variable ' X ' inaccessible here due to optimization, which is a small disadvantage of an editor with optimized functionality. If you want to check for variables that are easily optimized, you must turn off the compiler's optimization function, which is to turn off the optimization option on the compiler page in the Project Options dialog box.

A variable that is not initialized displays a random value until it is initialized.

Note

Watch list can be used as a simple decimal/16 binary converter. To convert a hexadecimal number to a decimal number, select Run | from the main menu. Add Watch ", click OK after entering the hexadecimal number in the Expression field, the hexadecimal number entered and the equivalent decimal number will be displayed in the Watch list; to convert a decimal number to a hexadecimal number, In addition to clicking Hexadecimal radio button to change the display type to 16, the remaining operations are the same as before. Since expression accepts a mathematical expression, watch list can also be used as a hexadecimal calculator, even with both hexadecimal and decimal values in the same expression. Such as:

There is only one drawback to using watch list, and the application must stop at a breakpoint .

Watch list is a simple and critical task in the process of debugging an application. To illustrate the use of watch list, do an exercise:

1) Create an application to place a button on its main form. Set the Name property of the button to the Watchbtn,caption property to watch Test. Change the main form Name property to the Debugmain,caption property to debug Watch List Test.

2) Double-click the button to display the button's onclick handling event in Code Editor and modify its onclick handler as follows:

123456789101112131415 procedure TDebugMain.WatchBtnClick(Sender: TObject);var  S: string;  X, Y: Integer;begin  X := Width;  S := IntToStr(X);  Y := Height;  X := X * Y;  S := IntToStr(X);  X := X div Y;  S := IntToStr(X);  Width := X;  Height := Y;end;

3) Save the project, name the unit Dbgmain, and the project is named Debugtest.

4) Set a breakpoint on the first line after the begin statement of the onclick handler to run the application.

5) Click the Watch Test button and the debugger will stop at the breakpoint. When the debugger stops at the breakpoint, the IDE and code Editor appear at the top level.

6) Add the variable s,x and y to watch list (because the code is optimized, the variable x and the variable y are inaccessible at the beginning), such as:

7) Position the watch list and code Editor so that the user can see two windows at the same time (you may want to dock the watch list to the bottom of Code Editor).

8) Switch the input focus to code Editor and press F8 to execute the next line of code, after execution, the execution point moves to the next line. The variable x in watch list displays a value. Such as:

9) Press F8 to continue the execution of the program on a line, monitoring the results of variables in watch list.

10) When the execution point reaches the last line of the onclick handler, click the Run button in the toolbar to continue running the application.

Users can repeatedly click the Watch Test button to see how watch list works, and each time you can experiment with the effects of different monitoring settings.

Note

In the example above, the onclick handler first obtains the value of the main form's width property and the Height property, performs some calculations, and then sets the width and height to the start value. Nothing has changed since this program was executed. Assigning a value to the width and height property at the end of the method is a reason.

If the variables x and y are not actually used in the code, the user cannot examine the two variables, because the compiler will find that the two variables are not used when optimizing the code, thus removing them. The two variables are used at the end of the method to prevent the compiler from optimizing them.

As mentioned above, I hope you have a basic understanding of the working process of the compiler with the optimization function. This way, when you start debugging your application, you won't be overwhelmed with messages such as "Variable ' Y ' inaccessible here due to optimization".

The above code is tested in Delphi7, the instance code is downloaded: watchlist test procedures-rar

Debugging of the Delphi Application (iii) monitoring variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.