1. steps:
A) add the checkmem. Pas unit to the project.
B) modify the project file and put 'checkmem. pa' to the first sentence of uses.
Program project1;
Uses
Checkmem in 'checkmem. pa ',
Forms,
Unit1 in 'unit1. pa' {form1}; // other unit files
{$ R *. Res}
Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End.
C) normal compilation and running of applicationsProgram
D) after exiting the application, a report will be generated in the application directory (if there is a vulnerability, no report will be generated if no vulnerability exists ).
Ii. Report explanation:
Report Content:
===== Project1.exe, 15:36:55 ====
available address space: 1024 KB (1048576 byte)
unsubmitted part: 1008 KB (1032192 byte)
submitted part: 16 KB (16384 byte)
idle part: 13 KB (14020 byte)
allocated part: 1 kb (2024 byte)
all small idle memory blocks: 0 KB (232 byte)
All idle memory blocks: 11 KB (11864 bytes)
other unused memory blocks: 1 kb (1924 bytes)
Memory Manager consumption: 0 KB (340 bytes)
address space loading: 0%
There are currently three memory vulnerabilities:
0) bytes 000000f300008-19 ($0013) bytes-not an object
1) bytes 000000f337a8-18 ($0012) bytes-not an object
2) bytes 000000f337b8-18 ($0012) bytes-not an object
Explanation:
There are currently three memory vulnerabilities: (three memory blocks are allocated but not released. Note that this is not the address of the object variable or pointer variable, it is the memory address of the object or the memory address pointed to by the pointer)
No. Is the address memory size of the memory not released an object? If the object name and class are listed and the object Implementation Unit file name is pointed out
0) bytes 000000f300008-19 ($0013) bytes-not an object
Iii. test example:
TestedCode:
Unit unit1;
Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
Stdctrls;
Type
Tform1 = Class (tform)
Button1: tbutton;
Button2: tbutton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
AA: tstringlist;
BB: tbutton;
End;
VaR
Form1: tform1;
Def: pointer;
Implementation
{$ R *. DFM}
Procedure tform1.button1click (Sender: tobject );
Begin
AA: = tstringlist. Create;
BB: = tbutton. Create (NiL );
AA. Add ('abcdefdafasdfasdfasdfasdf ');
Application. MessageBox (pchar (AA. Strings [0]), 'asdf ', mb_ OK );
// AA. Free;
End;
Procedure tform1.button2click (Sender: tobject );
VaR
P: pointer;
Begin
Getmem (def, 10 );
P: = def;
Fillchar (p, 10, $65 );
Application. MessageBox (def, 'aaa', mb_ OK );
// Freemem (def, 10 );
End;
End.
Click button1 first and then exit. The reports are as follows:
There are currently 10 Memory vulnerabilities:
0) bytes 000000f30000c-67 ($0043) bytes-not an object
1) 0000000000f0000a4-39 ($0027) bytes-not an object
2) bytes 000000f300008-55 ($0037) bytes-(untitled): tstringlist (48 bytes)-In classes. Pas
3) bytes 000000f337cc-518 ($0206) bytes-: tbutton (512 bytes)-In stdctrls. Pas
4) bytes 000000f339d0-42 ($ 002a) bytes-MS sans serif: tfont (36 bytes)-in graphics. Pas
5) bytes 000000f339f8-38 ($0026) bytes-(untitled): tsizeconstraints (32 bytes)-In controls. Pas
6) bytes 000000f33a1c-30 ($ 001e) bytes-(untitled): tbrush (24 bytes)-in graphics. Pas
7) bytes 000000f33a38-38 ($0026) bytes-not an object
8) bytes 000000f33a5c-38 ($0026) bytes-not an object
9) bytes 000000f33a80-42 ($ 002a) bytes-not an object
Comment out BB: = tbutton. Create (NiL); comment out: // BB: = tbutton. Create (NiL); Re-compile, run, and then click button1. The reports are as follows:
There are currently three memory vulnerabilities:
0) bytes 000000f300008-55 ($0037) bytes-(untitled): tstringlist (48 bytes)-In classes. Pas
1) bytes 000000f337cc-38 ($0026) bytes-not an object
2) bytes 000000f337f0-42 ($ 002a) bytes-not an object
Note: if an object is not released, multiple memory leaks will occur (because an object may contain multiple child objects)
OK, let's test button2 again (note that this time we don't click button1, only click button2 once). The generated report is as follows:
Currently, one memory vulnerability exists:
0) bytes 000000f300008-19 ($0013) bytes-not an object
Again, click button2 three times this time:
There are currently three memory vulnerabilities:
0) bytes 000000f300008-19 ($0013) bytes-not an object
1) bytes 000000f337a8-18 ($0012) bytes-not an object
2) bytes 000000f337b8-18 ($0012) bytes-not an object
This Note: For each unreleased memory, checkmem will be recorded! Note that the address of the above unreleased memory is next to each other. Therefore, if you see such a report, you can assume that it is a variable, allocated multiple times, but not released!
Iv. Memory Leak Testing and repair skills: (part of the content translated from memproof is not well translated. Please send us a letter for instructions)
The following are a couple of tips that can be usefull when fixing leaks in an application:
The following tips are useful for fixing application memory leaks:
* First just launch the app and then close it. if even this action generates leaks, fix those leaks first. only after the main leaks are fixed, you should go into specific functionality areas of the application.
* First, run the application and exit immediately. If this operation also results in Memory leakage, fix these vulnerabilities first. Only by fixing these major leaks can you test specific functions.
* In your Delphi/C ++ builder project options, remove as much forms as possible from the auto-create option. Create Your forms dynamically.
* In your Delphi/C ++ builder project options, do not use automatic creation forms as much as possible. You can create them dynamically when necessary.
* Pay attention to the code of the memory created or allocated in the loop. If they are not released, a large amount of memory leakage may occur.
* Go for the biggest classes first-if you see a tmyfom <class> leaking, try to fix this one first instead of going after a tiny tfont class. since a form will usually contain a lot of other classes, with one shot you will have fixed a lot of contained leaks.
* Modify the large class first. For example, if you see a tmyfom class leak, solve the problem first, and then solve small classes like tfont. A form class often contains multiple subclasses. Fix the unreleased issue of a form. You will solve the issue of a large number of sub-objects contained in the form that are not released.
* Go for the easy fixes first. Some leaks fixes are very easy and obvious-if you fix the easy ones first, you will keep them out of your way.
* First, fix vulnerabilities that are easy to fix. Some leaks are very easy to detect. If you fix them first, you don't have to think about them all the time.
Appendix: checkmem. Pas Unit
Unit checkmem; file: // Add it to the first line of project uses
Interface
Procedure snapcurrmemstattofile (filename: string );
Implementation
Uses
Windows, sysutils, typinfo;
Const
Maxcount = high (Word );
VaR
Oldmemmgr: tmemorymanager;
Objlist: array [0 .. maxcount] of pointer;
Freeinlist: integer = 0;
Getmemcount: integer = 0;
Freememcount: integer = 0;
Reallocmemcount: integer = 0;
Procedure addtolist (P: pointer );
Begin
If freeinlist> high (objlist) then
Begin
MessageBox (0, 'memory management monitor pointer list overflow, please increase the number of list items! ', 'Memory management monitor', mb_ OK );
Exit;
End;
Objlist [freeinlist]: = P;
INC (freeinlist );
End;
Procedure removefromlist (P: pointer );
VaR
I: integer;
Begin
For I: = 0 to freeinlist-1 do
If objlist [I] = p then
Begin
Dec (freeinlist );
Move (objlist [I + 1], objlist [I], (freeinlist-I) * sizeof (pointer ));
Exit;
End;
End;
Procedure snapcurrmemstattofile (filename: string );
Const
Field_width = 20;
VaR
OUTFILE: textfile;
I, currfree, blocksize: integer;
Heapstatus: theapstatus;
Item: tobject;
PTD: ptypedata;
PPI: ppropinfo;
Procedure output (Text: string; Value: integer );
Begin
Writeln (OUTFILE, text: field_width, value Div 1024, 'kb (', value, 'byte )');
End;
Begin
Assignfile (OUTFILE, filename );
Try
If fileexists (filename) then
Begin
Append (OUTFILE );
Writeln (OUTFILE );
End
Else
Rewrite (OUTFILE );
Currfree: = freeinlist;
Heapstatus: = getheapstatus; {local heap status}
With heapstatus do
Begin
Writeln (OUTFILE, '====', extractfilename (paramstr (0), ',', datetimetostr (now), '==== ');
Writeln (OUTFILE );
Output ('available address space: ', totaladdrspace );
Output ('uncommitted: ', totaluncommitted );
Output ('submitted: ', totalcommitted );
Output ('idle part: ', totalfree );
Output ('allocated part: ', totalallocated );
Output ('all small idle memory blocks: ', freesmall );
Output ('all large idle memory blocks: ', freebig );
Output ('other unused memory blocks: ', unused );
Output ('memory manager consumption: ', overhead );
Writeln (OUTFILE, 'address space loading: ': field_width, totalallocated Div (totaladdrspace Div 100),' % ');
End;
Writeln (OUTFILE );
Writeln (OUTFILE, format ('memory vulnerabilities at % d: ', [getmemcount-freememcount]);
For I: = 0 to currfree-1 do
Begin
Write (OUTFILE, I: 4, ')', inttohex (Cardinal (objlist [I]), 16 ),'-');
Blocksize: = pdword (DWORD (objlist [I])-4) ^;
Write (OUTFILE, blocksize: 4, '($' + inttohex (blocksize, 4) + ') byte ','-');
Try
Item: = tobject (objlist [I]);
If ptypeinfo (item. classinfo). Kind <> tkclass then {type info technique}
Write (OUTFILE, 'not an object ')
Else
Begin
PTD: = gettypedata (ptypeinfo (item. classinfo ));
PPI: = getpropinfo (ptypeinfo (item. classinfo), 'name'); {if it is tcomponent}
If PPI <> nil then
Begin
Write (OUTFILE, getstrprop (item, PPI ));
Write (OUTFILE ,':');
End
Else
Write (OUTFILE, '(not named ):');
Write (OUTFILE, item. classname, '(', PTD. classtype. instancesize,
'Byte)-in', PTD. unitname, '. pa ');
End
Except
On exception do
Write (OUTFILE, 'not an object ');
End;
Writeln (OUTFILE );
End;
Finally
Closefile (OUTFILE );
End;
End;
Function newgetmem (size: integer): pointer;
Begin
INC (getmemcount );
Result: = oldmemmgr. getmem (size );
Addtolist (result );
End;
Function newfreemem (P: pointer): integer;
Begin
INC (freememcount );
Result: = oldmemmgr. freemem (P );
Removefromlist (P );
End;
Function newreallocmem (P: pointer; Size: integer): pointer;
Begin
INC (reallocmemcount );
Result: = oldmemmgr. reallocmem (p, size );
Removefromlist (P );
Addtolist (result );
End;
Const
Newmemmgr: tmemorymanager = (
Getmem: newgetmem;
Freemem: newfreemem;
Reallocmem: newreallocmem );
Initialization
Getmemorymanager (oldmemmgr );
Setmemorymanager (newmemmgr );
Finalization
Setmemorymanager (oldmemmgr );
If (getmemcount-freememcount) <> 0 then
Snapcurrmemstattofile (extractfiledir (paramstr (0) + '\ checkmemory. log ');
End.