Delphi application debugging (9) debugging technology

Source: Internet
Author: User

Several debugging technologies will be introduced here. You can use these debugging technologies to make debugging easier.

Outputdebugstring Function

When the program is running, tracking program execution is sometimes very helpful to the user; maybe the user wants to view the value of the variable without using the breakpoint to pause the program execution. You can do this using the outputdebugstring function. This function is a convenient debugging tool, but many programmers ignore it because it is not introduced enough. Observe the last entry of the event log window, which is generated using the following code:

procedure TForm1.btn1Click(Sender: TObject);begin  OutputDebugString('In the btn1Click method...');end;

This is all to do. Since Delphi is a system debugger, any string sent using the outputdebugstring function will appear in the event log window. You can call the outputdebugstring function anywhere in the code.

To view the value of a variable, the string must be formatted and sent to the outputdebugstring function. For example:

After running the program, click btn1. The event log is shown as follows:

You can use the outputdebugstring function to view the entire process of running the program, even the code segment that requires strict practice.

 

Search for access violation

When a program tries to write data to memory that does not belong to it, Windows will issue an "access violation (access violation)" error message. All windows programmers have encountered access violation errors when developing applications.

Note

In 16-bit windows, use the term general protection error GPF (general protection fault ). This term is also popular in 32-bit windows, although 32-bit windows actually produces access violation error messages instead of GPF.

It is difficult for beginners and experienced windows programmers to track access violations. However, with the accumulation of experience in writing Windows programs, programmers will gradually find the cause of access violations to produce a sixth sense. The following describes some clues for your reference when looking for violations. Not only such cases can cause access violations for programs, but they are the most common cases.

1. pointer not initialized

The uninitialized pointer is a pointer already declared in the program, but it has not been assigned a meaningful memory address value. The uninitialized pointer contains random data. The best case is that it points to an insignificant address in the memory. The worst case is that it points to a unit in the memory where the user program is located, this will lead to unstable program actions, because the pointer may point to different memory units each time you run the program. The pointer should be set to nil after a pointer is used and the object indicated by the pointer is deleted ). When a null pointer is accessed, the program stops running and reports an access violation. The debugger is highlighted to display the problematic source code line, so that the user can immediately find the problematic pointer.

2. Delete the previously deleted pointer.

Deleting a deleted pointer will cause an access violation. The deleted pointer should be set to nil (NULL pointer); it is safe to delete a null pointer. Set the deleted pointer to nil (NULL pointer), and then delete the pointer without any errors.

3. array overwrites)

Overwriting the end of an array can cause access violations. In some cases, the covered memory is not critical and will not immediately show problems, but soon the program stops running due to a fault. In this case, the user may find the fault where the program stops running, but the problem is actually found elsewhere in the program. Another scenario is that the covered memory is critical, so the program stops running immediately due to a fault. In extreme cases, Windows may be damaged.

You can use the range check to minimize the possibility of array coverage. When the check range is set (the default setting), the compiler checks Each array reference to see if the accessed array element is out of the valid range. For example, the following code will cause the compiler to report an error:

In this Code, you need to access element 30 of an array, which has only 21 elements. The compiler checks that the accessed array element is out of the range declared by the array and generates a compiler error. The error message is as follows:

However, the check range is invalid for the variable. For example, the following code will not cause compilation errors:

In this Code, although the array contains nine bytes, no compilation error is generated because the compiler does not know the value of variable X during compilation.

4. access violation upon program termination

An access violation occurs when a program is closed normally, which generally indicates that the stack settings are too small. Although this is unlikely to happen in 32-bit programs, it can also happen in extreme cases. As mentioned earlier, deleting a deleted pointer may also cause access violations when the program is terminated.

Prompt for debugging

In addition to the many tips given above, you also need to add the following tips:

  • Change the caption attribute of the form to display variables without using breakpoints. Because it is easy to add a label component to a form, you can use the label component to change the body of the label component to display the value of the variable or other information to be displayed.
  • Enable a conditional breakpoint or data monitoring breakpoint to temporarily slow down the program running speed (for example, to make the program run slowly to view the program effect ). When a program runs on a breakpoint, it must check the breakpoint conditions to slow down the program execution.
  • When the program is running, use the evaluate/modify dialog box to temporarily change the value of a variable. In this way, you can view the impact of different values on the program, instead of re-compiling the code each time.
  • Select Run | inspect from the main menu and enter self in the expression field to check the classes currently paused by the debugger.
  • Messagebeep ($ FFFF) is used as the occurrence indicator to indicate that the program execution reaches a certain position in the program. When $ FFFF is used, it will make the PC speaker beep.
  • Select the run | program reset menu item from the main menu or press Ctrl + F2 to terminate the error debugging process.
  • Use intermediate variables to disconnect long equations or chained method calls so that you can check the results more conveniently.
  • Use the showmessage, MessageBox, or messagedlg function to display program trace information (showmessage function is more convenient because it has only one string parameter ).
Caution

If you are running delphi on Windows 95, use the program reset option as few as possible. In some cases, terminating the application using the program reset will destroy Windows 95.

But not all windows 95 will have this situation, so this problem may not occur. Windows NT is less likely to have this problem, so you can safely use program reset in Windows NT. Personally, I use program reset only when the debugged application is locked.

Another prompt is that a memory check program can be used when you debug the program. Third-party memory check programs are used to check whether user applications have memory leaks. This type of program saves a lot of trouble when users put applications into practical use. When the application memory leaks, problems may occur when using the application. Early detection and elimination of memory leaks can undoubtedly greatly save your time and effort.

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.