C ++ Builder Memory Leak Check Tools - Codeguard

zhaozj2021-02-16  53

About how to use Codeguard in BCB

First, why writing this thing

I need to check a lot of things when using BCB5 to write some programs, such as memory leaks, resource is released, etc. After using a lot of tools, I found that the tools of BCB5 itself comes with --codeguard, very good, very convenient However, it took a long time (and overturned some information, including Help). The purpose of writing this article hopes that there is a friend of this issue can learn from each other, and everyone will learn together and make progress together. My contact method: email: szbug@szbug.com, I hope that the like-minded friend will communicate with each other. The following article is a piece of article that is patching, some information is found on the book, some are seen on Help.

Second, what is codeguard

CodeGuard is a tool that appears in C Builder5. CodeGuard is a program in C Builder in runtime inspector for checking memory or resource usage, and verification of function calls.

CodeGuard can detect the following program running period error:

l illegal memory release.

l Invalid handle or file stream.

l illegally pointed.

l Use the pointer that has been released.

l Memory leakage.

l Assign but finally there is no memory variable.

l Pass the incorrect parameters of the function (including VCL and Win32 functions).

l Function Return the error. (Including VCL and Win32 functions).

For example, attempting to release the same resource multiple times in the application (or have released resources), trying to access memory that has been released.

Third, how to use codeguard in BCB5 - Configure Codeguard

If you want to use Codeguard, some code must be compiled into your application, so after changing the following settings. You must recompile all (remember to remember !!!). First, open the Codeguard page box of the application's engineering option, and tick the CodeGuard Validation in front of the project option, there are other three options. The first option allows the CodeGuard to check the invalid pointer and data overflow to the local, global and static variable. The second option allows CodeGuard to detect calls for methods of illegal (invalid, deleted) objects. The third option allows the CodeGuard to verify access to the inline pointer (in some information, open this option will cause the program execution speed to be very slow, I tested, if the project is not big, it is not very obvious, you can accept .) General debugging is to open all options (default selection is all open).

With the Codeguard configuration tool, you can configure some options for CodeGuard to perform CGConfig.exe in the command line. You can see a dialog

Preferences tab is used to set global options for CODEGUARD. Enable options can be used in the case where the application does not recompile or does not use Codeguard, which is generally enabled. If you use CODEGUARD, it is recommended to set the project option to disable or use Codeguard. The Stack Fill Frequency fill stack is to detect invalid access to the running stack. The Report and Error Message Box options are how to set up the Codeguard report error. In Report, the Stiatistics option opens the CODEGUARD output allocation and release memory statistics, the Win32API call, the usage of resources, and add a module list in the log file to check errors. The Resource Leaks option is to tell CodeGuard to report the resource leak after the application ends. After the Error Message Box option is selected, when the application is not running in the IDE, if the CodeGuard detects an error message, the user will tell the user using a dialog box. Other options are generally unused, you can see the online HELP of C Builder. The Resource Options and Function Options page in the CodeGuard Configuration Tool allows users to set various tracking options for the application's resources, files, and function calls. Unless special reasons need to change the default configuration, you will use the default settings. There is a relatively common option on the Function Options page that is a record of a specific function.

The Ignored Modules page box allows you to tell CodeGuard that some running periods can be ignored when detecting (some DLLs or packages) are ignored. This option is generally not commonly used.

Fourth, use codeguard

Using Codeguard is actually very simple, just configured CodeGuard as before, then runs your application, whether your application is running in the IDE, CodeGuard will monitor your application according to the options configured in the Codeguard. At the same time, he will also output all information in a log file (file storage in the directory of your project, the file name and engineering name, the extension is .cgl). For example, your project is called C: /word/test.prg, then the Codeguard log file is C: /word/test.cgl, which is a text file that can be edited with any text editor.

In the IDE, you can view the codeguard log file (or use shortcut Ctrl ATL O) via

view-> debug window-> codeguard log.

If your program is running an error that is a CODEGUARD monitoring, CodeGuard will output it into the CodeGuard log. And the error message is displayed in a "tree" (easy to use, just as simple as using Windows resource manager). Each error can be expanded to display some information specific to a certain error type. For example, a resource uses, allocates, and releases; stack information when an error occurs; and pointed out the error of the error. This will find the wrong root of the wrong!

There are two buttons on the CodeGuard log window. When the STOP is selected, if the program encounters an error at this time, CodeGuard will stop the application. If not, the program will continue if the program encounters an error, which can run a lot of error messages once. When the CLEAR is selected, the application will clear the information in the log in a timely.

In the CodeGuard log window, when there is a source code, the IDE window will automatically jump onto the line of code if there is a source code. If there is no source code, the CPU window is displayed. In Figure 3, an error occurs is a resource leak. When your mouse double-click TFORM1: Button1Click, it will automatically jump to the row of the error in the source code. When CodeGuard detects an error, and find the source code that appears, the rest is if the job is correcting your code. This process can be achieved with monitoring and data breakpoints, the effect is better!

V. Errors and causes in Codeguard

CodeGuard can detect a lot of runtime errors! Usually it is easy to find an error from the meaning of CodeGuard. For most errors, Codeguard generally displays: places, resource allocation, resource release, resource allocation, and number of visited bytes.

ACCESS IN FREED MEMORY

This error occurs if the memory is released. In C / C , New or Malloc is usually allocated, released with Delete and Free. 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 to be released, where the memory is originally assigned, and where there is free.

2. Method Called on Freed Object

This error is similar to the previous error. The cause is due to the way to call the object that has been released instead of accessing the memory that has been released!

Void foo ()

{

TMYCLASS * MyClass = New TMYCLASS ();

Delete myclass;

Myclass-> xxxx (10);

}

CodeGuard will display where the method of the released object has been displayed, the object being created, and the object being released.

3. Reference to Freed Resource

Attempting multiple times in the program (more than twice) release the same resource, CodeGuard will detect this error, and there are several ways to generate this error! E.g:

Void foo ()

{

TMYCLASS * MyClass = New TMYCLASS ();

Delete myclass;

Delete myclass;

}

CodeGuard will be released for the second time in the reporting resource, causing this error. It also reports where resources are allocated, where is the first release.

4. Method Called ON ILLEGALLY CASTED OBJECT

This error will occur if the call to the method outside the program will cause this error.

Void foo ()

{

TMYCLASS * Myclass = New TMYCLASS [5];

Myclass [5] .xxxx (); // no Such myclass [5]

delete [] myclass;

}

CodeGuard will report the object that the object call is defined, and the place where this method is called and the object or memory is allocated.

5. Resource Type Mismatch

This error will occur if the resource and definition (allocation) are released in the program.

Void foo ()

{

TMYCLASS * Myclass = new TMYCLASS [2]; delete myclass; // code1

TMYCLASS * MyClass = New TMYCLASS ();

Delete [] myclass; // code2

}

Both Code1 and Code2 trigger a Resource Type Mismatch error, and CodeGuard will report where resources are released in inconsistent ways, and where the resource is all assigned.

6. Access overrun

This error is caused when the memory of the illegal memory area is accessed (after the inside of the accessible memory area), usually, the array subscript is exceeded to be defined.

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 where the resource is allocated.

7. Access Underrun

This error is caused when the memory of the illegal memory area is accessed (before the internal existence of the legal memory area).

Void foo ()

{

TMYCLASS * MyClass = New Tmyclass [2];

Myclass [-1] .ABC = 10; // no Such myclass [2]

delete [] myclass;

}

CODEGUARD reports where the resource is allocated.

8. Uninitialized Stack Accessing

The area where the access stack is initialized will cause this error.

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 yet.

9. Access in invalid stack

This error occurs when you try to access the memory at the bottom of the stack!

Void foo ()

{

Char Str [20];

STRCPY (& STR [-1], "szbug");

}

The CodeGuard reports the wrong place.

10. Bad Parameter

This error is usually an invalid file or other resource handle as a parameter passed to the VCL or Win32API function.

Void foo ()

{

File * stream;

Fclose (stream);

}

CodeGuard is called where the report uses the function of incorrect parameters.

11. Function Failure

This error is that the return value of the CodeGuard in capture VCL and the Win32API function is thrown when an error occurs.

Viod foo ()

{

CopyFile ("c: /abc/abc.txt", "d: /abc/acb.txt", true);

// If this function fails for some reason,

// then CodeGuard will capture and report Function Failure Error!

}

12. Resource Leak

If resources (including WINWOS resources, memory resources, etc.) are allocated in programs, in the final un release! Resource Leak errors will be triggered.

Void foo ()

{

Char * ch = new char [10];

}

CodeGuard will report the place where resources are created, as well as the number of bytes leaked.

After the sixth operation, it will produce the same name's CGL file, including the number of calls to the function and the use of DLL. If there is a disclosure, it will point out !!!!

Functions Called: Delete (35 Times) SysfreeMem (26 Times) sysfreemem (464 Times) SysgetMem (472 Times) Realloc (1 Times) Memcpy (1 Times) Delete [] (2 Times) Free (26 Times) New [] (14 NEW (40 TIMES) Calloc (5 Times) Malloc (20 Times) Resource Types Used: Object Array (14 Allocs, 13 Max) Object (40 Allocs, 28 Max) modules used: 00400000 02/07/2003 09:56 : 24 D: /Project1.exe 01190000 02/01/2002 22:00:00 C: / Program files / borland / delphi7 / bin / borlndmmmmmm.dll 0cd00000 02/01/2002 22:00:00 C: / progra ~ 1 / borland / cbuild ~ 1 / bin / cg32.dll 10000000 03/09/2001 18:42:32 c: /winnt/mui/fallback/0404/msctf.dll.mui 32600000 08/20/2002 16:40: 24 C: /Winnt/System32/CC3260MT.DLL 37210000 12/28/2002 18:25:22 C: /Winnt/downlo ~ 1/cnsmin.dll 40000000 10/21/2002 06:03:00 C: / WinNT / SYSTEM32 / RTL60.BPL 400B0000 02/01/2002 22:00:00 C: /Winnt/System32/VCL60.BPL 60000000 03/09/2001 17:06:24 C: /Winnt/SYSTEM32 - MsCTF.DLL 70BD0000 08 / 29/2002 09:33:44 C: /Winnt/system32/shlwapi.dll 71710000 08/29/2002 09:33:44 C: / Winnt / System32 / COMC Tl32.dll 74fa0000 01/10/2000 20:00:00 C: /Winnt/System32/ws2help.dll 74fb0000 07/22/2002 12:05:04 C: /Winnt/System32/WS2_32.DLL 74FD0000 07/22 / 2002 12:05:04 C: /Winnt/system32/wsock32.dll 75010000 07/22/2002 12:05:04 c: /winnt/system32/mpr.dll 75280000 01/10/2000 20:00:00 C: / Winnt/System32/oledlg.dll 75950000 01/10/2000 20:00:00 c: /winnt/system32/lz32.dll 75e00000 07/22/2002 12:05:04 c: /winnt/system32/imm32.dll 76AF0000 07/22/2002 12:05:04 C: /Winnt/System32/Comdlg32.dll 777C0000 11/01/2002 16:41:30 C: /Winnt/System32/winspool.drv 777e0000 01/10/2000 20: 00:00 C: /Winnt/System32/Version.dll 77990000 07/22/2002 12:05:04 C: /Winnt/system32/oleaut32.dll 77a30000 07/22/2002 12:05:04 C: / Winnt / System32 / ole32.dll 77d90000 11/11/2002 15:34:36 C:

/ Winnt/System32/advapi32.dll 77df0000 11/04/2002 10:59:22 C: /Winnt/System32/User32.dll 77e60000 11/04/2002 10:59:30 C: /Winnt/System32/kernel32.dll 77F40000 07/23/2002 16:34:08 c: /winnt/system32/gdi32.dll 77f80000 07/22/2002 12:05:04 C: /Winnt/System32/NTDLL.DLL 78000000 07/22/2002 12: 05:04 C: /Winnt/system32/msvcrt.dll 786f0000 07/22/2002 12:05:04 c: /winnt/system32/rpcrt4.dll 78f90000 12/11/2002 17:50:30 C: / WinNT / System32 / shell32.dll ==========================================, codeguard You can also detect a lot of errors in the application, just talk about some common errors, other errors and examples, please see the HELP of C Builder. I hope everyone can find out the mistakes in the program through CodeGuard and bugs! ! ! I hope that everyone's procedures are getting stronger and more stable. . . Ha ha

转载请注明原文地址:https://www.9cbs.com/read-23743.html

New Post(0)