About how to use codeguard in BCB)

Source: Internet
Author: User

FM: http://www.189works.com/article-42035-1.html

How to Use codeguard in BCB

Author: Shenzhen insects from: Shenzhen wormhole net
This article from http://www.szbug.com/disparticle.aspID=4

I. Why do I write this article?
When using bcb5 to write some programs, you need to check a lot of things, such as memory leaks and whether resources are released. After using many tools, you find that the tool of bcb5 itself-codeguard, it is very good and convenient to use, but after a long time of exploration (and some information, including help), it can be used. The purpose of writing this article is to help you learn from each other and make progress together. My contact method: Email: szbug@szbug.com, hope like-minded friends to communicate with each other. The following is a piece of article. Some materials are found in books and some are seen in help. First of all, I declare that my Chinese and English skills are very limited. I hope to point out something wrong or wrong. Special thanks to XX's love for helping me proofread ......
2. What is codeguard?
Codeguard is a tool that appears only when it is C ++ builder5. Codeguard is a runtime checker of a program in C ++ builder. It is used to check memory or resource usage and function call verification.
Codeguard can detect the following program running errors:
Invalid Memory release.
Invalid handle or file stream.
Invalid Pointer.
Use released pointer.
Memory leakage.
Memory variables allocated but not released at the end.
Invalid parameters passed to the function (including VCL and Win32 functions ).
Function return value error. (Including VCL and Win32 functions ).
For example, you try to release the same resource (or released resource) multiple times in an application and access the released memory.
3. How to Use codeguard in bcb5 -- configure codeguard
To use codeguard, some code must be compiled into your application, so after you change the following settings. All must be re-compiled (remember to remember !!!). 1. Open the codeguard page of the project option of the application and check the front of codeguard validation (see figure 1 ).

1. There are three other options in the project options. The first option allows codeguard to check invalid pointers and data overflow pointing to local, global, and static variables. The second option allows codeguard to detect calls to methods of illegal (invalid, deleted) objects. The third option allows codeguard to verify the access of embedded pointers (in some documents, enabling this option will slow program execution and I tested it, if the project is not large, it is not obvious and acceptable .) General debugging is to open all the options (the default option is also to open all ).
With the codeguard Configuration tool, you can configure some codeguard options and execute cgconfig. EXE in the command line mode. You can see a dialog box (figure 2)
2. The Preferences tab is used to set the global options of the codeguard tool. The enable option can be used without re-compiling the application or codeguard. Generally, it is enabled. If codeguard is used, we recommend that you set project options to disable or use codeguard. Stack fill frequency filling stack frequency is used to detect invalid access to the runtime stack. The report and error message box options are used to set codeguard to report errors. In the report, the stiatistics option opens the codeguard output statistics table for memory allocation and release, the WIN32API call in use, and the resource usage, and adds a module list to the log file, to check for errors. Resource
The leaks option tells codeguard to report resource leaks after the application ends. After the error message box option is selected, if codeguard detects an error message when the application is not running in IDE, a dialog box is used to notify the user. Other options are not commonly used. For more information, see the online help of C ++ builder.
The resource options and function Options page of The codeguard Configuration tool allows you to set various trace options for application resources, files, and function calls. Unless you need to change the default configuration for special reasons, you can use the default settings. A common option on the function Options page is to record each call of a specific function.
The ignored modules page allows you to tell codeguard that some runtime errors can be ignored during detection (generally some DLL or package ). This option is not commonly used.
4. Use codeguard
Codeguard is actually easy to use. You only need to configure codeguard as before, and then run your application, whether or not your application runs in the IDE, codeguard will monitor your application according to the options configured by codeguard. At the same time, he will output all the information to a log file (the file is stored in the directory where your project is located, the file name is the same as the project name, the extension is. CGL ). For example, if your project name is C: wordtest. PRG, The codeguard log file is C: wordtest. CGL. It is a text file and can be edited using any text editor.
In IDE, you can use <menu> View> debug WINDOW> codeguard log to view the codeguard log file (or press Ctrl + ATL + O ).
If your program encounters an error that is monitored by codeguard, codeguard will output it to the codeguard log. And display the error information in a "Tree" (easy to use, just as using Windows resource manager ). Each error can be expanded to display information specific to an error type. For example, a resource is used, allocated, and released, stack information in the case of an error, and an error code line is pointed out. In this way, you can quickly find the root cause of the error!

The codeguard log (Figure 3) window has two buttons stop and clear. When the stop option is selected, if the program encounters an error at this time, codeguard will stop the application. If not selected, the program will continue even if an error occurs, so that many error messages can be recorded at a time. When clear is selected, the information in the log is cleared every time the application re-runs.
In the codeguard log window, when you double-click a single wrong node, if the source code exists, the IDE window will automatically jump to that line of code. If the source code does not exist, the CPU window is displayed. Figure 3 shows a resource leakage error. When you double-click the tform1: button1click line, it automatically jumps to the line with the error in the source code.
When codeguard detects an error and finds the source code, the rest of the work is to correct your code. This process can be achieved with monitoring and data breakpoint, and the effect is better!
5. Errors and causes in codeguard)
Codeguard can detect many runtime errors! It is usually easy to find out the root cause of the error from the meaning of codeguard. For most errors, codeguard generally displays errors, resource allocation, resource release, resource allocation, and number of accessed bytes.
1. Access in freed memory
If the memory is released and the Access continues later, this error occurs. In C/C ++, new or malloc is usually used to allocate memory, and delete and free are used to release memory. The following is an example of accessing the released memory:
Void Foo ()
{
Tmyclass * myclass = new tmyclass ();
Delete myclass;
Myclass-> XXXX = 10; // myclass has been released
}
Codeguard will report where the released inner is accessed, where the memory is originally allocated, and where the inner is released.
2. method called on freed object
This error is similar to the previous one. The reason is that the method of the released object is called instead of accessing the released memory!
Void Foo ()
{
Tmyclass * myclass = new tmyclass ();
Delete myclass;
Myclass-> xxxx (10 );
}
Codeguard displays Where the Released object method is called, where the object is created, and where the object is released.
3. Reference to freed Resource
Codeguard will detect this error when trying to release the same resource multiple times (more than twice) in the program. There are several methods to generate this error! For example:
Void Foo ()
{
Tmyclass * myclass = new tmyclass ();
Delete myclass;
Delete myclass;
}
Codeguard reports where the resource is released for the second time, causing this error. It also reports where resources are allocated and where they are released for the first time.
4. method called on illegally casted object
This error occurs if you call a method beyond the valid memory range in the program.
Void Foo ()
{
Tmyclass * myclass = new tmyclass [5];
Myclass [5]. xxxx (); // no such myclass [5]
Delete [] myclass;
}
Codeguard will report the definition of the method called by the object, the place where the method is called, and the place where the object or memory is allocated.
5. Resource Type Mismatch
This error occurs if the resources released in the program are inconsistent with the definition (allocation.
Void Foo ()
{
Tmyclass * myclass = new tmyclass [2];
Delete myclass; // code1
Tmyclass * myclass = new tmyclass ();
Delete [] myclass; // code2
}
Both code1 and code2 will cause the Resource Type Mismatch Error. codeguard will report where the resource is released in an inconsistent way and where the resource is allocated.
6. Access Overrun
This error occurs when you access the memory in the invalid memory area (after the accessed memory area exists in the valid memory area). Generally, the array subscript reference exceeds the original definition.
Void Foo ()
{
Tmyclass * myclass = new tmyclass [2];
Myclass [2]. ABC = 10; // no such myclass [2]
Delete [] myclass;

Char * Ch = new char [5];
Strcpy (CH, "123456"); // Error
Delete [] ch;
}
Codeguard reports errors and where resources are allocated.
7. Access underrun
This error occurs when you access the memory in the invalid memory area (before the accessed memory area exists ).
Void Foo ()
{
Tmyclass * myclass = new tmyclass [2];
Myclass [-1]. ABC = 10; // no such myclass [2]
Delete [] myclass;
}
Codeguard reports errors and where resources are allocated.
8. uninitialized stack accessing
This error occurs when the region is initialized in the access stack.
Void foo1 (INT ** PTR)
{
Int var;
* PTR = & var;
}
Void Foo ()
{
Int * PTR;
Foo1 (& PTR );
* PTR = 100;
}
Codeguard will report where to access the stack that has not been initialized.
9. Access in invalid Stack
This error occurs when the program attempts to access the memory at the bottom of the stack!
Void Foo ()
{
Char STR [20];
Strcpy (& STR [-1], "szbug ");
}
Codeguard reports errors.
10. Bad Parameter
This error usually occurs when invalid files or other resource handles are passed as parameters to VCL or WIN32API functions.
Void Foo ()
{
File * stream;
Fclose (Stream );
}
Codeguard will report where the function with incorrect parameters is called.
11. function failure
This error is caused by codeguard when an error occurs when the returned values of VCL and WIN32API functions are captured.
Viod Foo ()
{
Copyfile ("abc.txt", "acbd.txt", true );
// If this function fails for some reason,
// Then codeguard will capture and report the function failure error!
}
12. resource leak
If resources in the Program (including winwos resources and memory resources) are allocated, they are not released at the end of the program! Resource leak error.
Void Foo ()
{
Char * Ch = new char [10];
}
Codeguard reports where resources are created and the number of leaked bytes.
6. codeguard can also detect many application errors. Here we only talk about some common errors. For other errors and examples, see the help of C ++ builder. I hope you can use codeguard To Find Out program errors and bugs !!! I hope that everyone's programs will become stronger and more stable...

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.