Windows Mobile Memory leakage detection

Source: Internet
Author: User

Memory detection in Windows Mobile. Some tools, such as appvertifyer, do not actually work well, but more require programmers to add detection code during coding, the following class can be used to detect the memory in Windows Mobile, mainly to rewrite new and delete, and then add the macro definition to the file to be detected.

 

The header code of crtdbg. H is as follows:

/*************************************** ************************* </P> <p> filename: crtdbg. h </P> <p> author: Ciprian miclaus (Ciprian_Miclaus@yahoo.com) </P> <p> description: <br/> detects memory leaks in EVC ++ almost the same way crtdbg does in VC ++. <br/> at the end of program execution it will display in the debug window if <br/> there were any memory leaks and how the memory looks so you can identify <br/> Where your memory leak occurred. it will display in the debug window a <br/> message saying no memory leaks detected if there are no memory leaks. <br/> similar to what crtdbg does in VC ++. <br/> it will fail to display the line same as crtdbg does. <br/> there are 3 simple steps in order to enable Memory Leak Detection: <br/> 1. define _ debug <br/> # DEFINE _ debug <br/> 2. include "crtdbg. H "<br/> # inclu De "crtdbg. H "<br/> 3. let your first line in the code be: <br/> _ crtsetdbgflag (on); </P> <p> tips on debugging: <br/> tip 1: <br/> altho it doesn't display the line where the memory leak occurred (read <br/> Tip 2), the utility display the address in hexa, and you can add a small <br/> code to the operator new function, just after the first malloc: <br/> If (retptr = (void *) 0x76da0) <br/> dumb instruction; <-Place a breakpoint on this one <br/> so you can detect easily which line of your code called the operator new <br/> to allocate memory at the specified address and wasn' t freed. <br/> Tip 2: <br/> here's a trick that allow you to get the correct line and filename where <br/> the memory leak occurred. define the following line in every file, or define <br/> it in. H and include it in every file wh Ere you want accurate line and <br/> filename: </P> <p> # define new (_ T (_ file _), _ line __) </P> <p> happy debugging! </P> <p> license: public domain </P> <p> comments: <br/> Please report any bugs to Ciprian_Miclaus@yahoo.com. <br/> you can use and distribute this code freely, but please keep these <br/> few lines. <br/> if you make any improvements, or have any ideas about how this Code <br/> cocould be improved or just you feel you need to comment this code in <br/> Any way, please send your comments, idea, imporvements Me To My <br/> email above. <br/> The code doesn't detect memory leaks generated with C functions: <br/> malloc, calloc, free, but that can be done in the future. let me know <br/> and I will program it. </P> <p> ********************************* *********************************/</P> <p> # ifndef _ crtdbg_header <br/> # DEFINE _ crtdbg_header </P> <p> # ifdef _ debug </P> <p> # include <stdlib. h> <br/> # include <malloc. H> <br/> # include <string. h> <br/> # include <tchar. h> </P> <p> # include <dbgapi. h> </P> <p> // un-comment this line if you get an error: <br/> // unresolved... nkdbuplintfw... <br/> // extern "C" Void winapiv nkdbuplintfw (lpwstr lpszfmt ,...); </P> <p> struct _ crtfilename {<br/> unsigned short * _ crtname; <br/> _ crtfilename * _ crtnext; <br/> }; </P> <p> struct _ crtmem {<br/> _ crtfilename * _ crtfilename; <br/> int_crtline; <Br/> unsigned int_crtmemlen; <br/> void * _ crtmemaddr; <br/> _ crtmem * _ crtnext; <br/> }; </P> <p> void * operator new (<br/> unsigned int S, <br/> unsigned short * Name, <br/> int line <br/>); </P> <p> inline void * _ cdecl operator new (unsigned int s) <br/> {return :: operator new (S, _ T (_ file _), _ line __);} </P> <p> void _ cdecl operator Delete (void * pvmem); </P> <p> class garbagecollector {<br/> Public: <br/> garb Agecollector () {}< br/> ~ Garbagecollector (); <br/>}; </P> <p> # DEFINE _ crtsetdbgflag (ignore)/<br/> garbagecollectorgb; </P> <p> void * operator new (unsigned int S, unsigned short * Name, int line ); </P> <p> void _ cdecl operator Delete (void * pvmem ); </P> <p> # else </P> <p> # DEFINE _ crtsetdbgflag (ignore) </P> <p> # endif // debug </P> <p> # endif // Header

 

The crtdbg. CPP Code is as follows:

# Include "stdafx. H "<br/> # include" crtdbg. H "</P> <p> _ crtmem * _ crtmemroot = 0; <br/> _ crtfilename * _ crtfilenameroot = 0; <br/> unsigned longmaxmem = 0; <br/> unsigned longcurrmem = 0; </P> <p> void * operator new (<br/> unsigned int S, <br/> unsigned short * Name, <br/> int line <br/>) <br/> {<br/> void * retptr = malloc (s); <br/> If (retptr) {<br/> currmem + = s; <br/> If (currmem> maxmem) <br/> {<br/> maxmem = C Urrmem; <br/>}< br/> _ crtmem * _ crtmemcell = (struct _ crtmem *) malloc (sizeof (_ crtmem )); <br/> _ crtmemcell-> _ crtline = line; <br/> _ crtmemcell-> _ crtmemlen = s; <br/> _ crtmemcell-> _ crtmemaddr = retptr; <br/> _ crtmemcell-> _ crtnext = 0; </P> <p> _ crtfilename * _ tmpcrtfilename; <br/> for (_ tmpcrtfilename = _ crtfilenameroot; _ tmpcrtfilename & wcscmp (name, _ tmpcrtfilename-> _ crtname); _ tmpcrtfilename = _ tmpcrtfilename-> _ Crtnext); <br/> If (! _ Tmpcrtfilename) {<br/> unsigned short * _ crtname = (unsigned short *) malloc (wcslen (name) + 1) * sizeof (unsigned short )); <br/> wcscpy (_ crtname, name); <br/> _ crtfilename * _ crtfilename = (struct _ crtfilename *) malloc (sizeof (_ crtfilename )); <br/> _ crtfilename-> _ crtname = _ crtname; <br/> _ crtfilename-> _ crtnext = 0; <br/> If (! _ Crtfilenameroot) <br/> _ crtfilenameroot = _ crtfilename; <br/> else {<br/> for (_ tmpcrtfilename = _ crtfilenameroot; _ tmpcrtfilename-> _ crtnext; _ tmpcrtfilename = _ tmpcrtfilename-> _ crtnext); <br/> _ tmpcrtfilename-> _ crtnext = _ crtfilename; <br/>}< br/> _ tmpcrtfilename = _ crtfilename; <br/>}< br/> _ crtmemcell-> _ crtfilename = _ tmpcrtfilename; </P> <p> If (! _ Crtmemroot) {<br/> _ crtmemroot = _ crtmemcell; <br/>}< br/> else {<br/> _ crtmem * _ tmpmemptr; <br/> for (_ tmpmemptr = _ crtmemroot; _ tmpmemptr-> _ crtnext; _ tmpmemptr = _ tmpmemptr-> _ crtnext ); <br/> _ tmpmemptr-> _ crtnext = _ crtmemcell; <br/>}</P> <p> return retptr; </P> <p >}</P> <p> void _ cdecl operator Delete (void * pvmem) <br/>{< br/> If (pvmem) {<br/> _ crtmem * _ tmpmem; <br/> If (pvmem = _ crtmemroot-> _ crtmem ADDR) {<br/> _ tmpmem = _ crtmemroot; <br/> _ crtmemroot = _ crtmemroot-> _ crtnext; <br/> currmem-= _ tmpmem-> _ crtmemlen; <br/> free (_ tmpmem ); <br/>}< br/> else {<br/> for (_ tmpmem = _ crtmemroot; _ tmpmem-> _ crtnext & (_ tmpmem-> _ crtnext-> _ crtmemaddr! = Pvmem); _ tmpmem = _ tmpmem-> _ crtnext); <br/> If (_ tmpmem-> _ crtnext) {<br/> _ crtmem * _ tmpmem2; <br/> _ tmpmem2 = _ tmpmem-> _ crtnext; <br/> _ tmpmem-> _ crtnext = _ tmpmem2-> _ crtnext; <br/> currmem-= _ tmpmem2-> _ crtmemlen; <br/> free (_ tmpmem2 ); <br/>}< br/> else <br/> nkdbuplintfw (_ T ("% s (% I): Warning: deletes memory pointer not allocated with new! /N "), _ T (_ file _), _ line _); <br/>}< br/> free (pvmem ); <br/>}</P> <p> garbagecollector ::~ Garbagecollector () <br/>{< br/> If (! _ Crtmemroot) <br/> nkdbuplintfw (_ T ("no memory leaks detected! /N "); <br/> else {<br/> _ crtmem * _ tmpmem; <br/> nkdbuplintfw (_ T (" detected memory leaks! /Ndumping objects->/N "); <br/> for (_ tmpmem = _ crtmemroot; _ tmpmem = _ tmpmem-> _ crtnext) {<br/> nkdbuplintfw (_ T ("% s (% I): normal block at 0x % 08x, % I bytes long/n data <"), _ tmpmem-> _ crtfilename-> _ crtname, _ tmpmem-> _ crtline, _ tmpmem-> _ crtmemaddr, _ tmpmem-> _ crtmemlen ); <br/> unsigned int _ length = _ tmpmem-> _ crtmemlen <100? _ Tmpmem-> _ crtmemlen: 50; <br/> for (unsigned int I = 0; I <_ length; I ++) <br/> nkdbuplintfw (_ T ("% C"), * (char *) _ tmpmem-> _ crtmemaddr) + I )); <br/> nkdbuplintfw (_ T (">/N"); <br/>}< br/> _ crtfilename * _ tmpname = _ crtfilenameroot; <br/> for (; _ tmpname;) {<br/> _ crtfilenameroot = _ tmpname-> _ crtnext; <br/> free (_ tmpname-> _ crtname ); <br/> free (_ tmpname); <br/> _ tmpname = _ crtfilenameroot; <br/>}</P> <p> nkd Buplintfw (_ T ("Maximum free store memory allocated at a time: % lu! /N "), maxmem); </P> <p>}

 

 

Then add _ crtsetdbgflag (on) to the main function of the program. In fact, this is a # DEFINE _ crtsetdbgflag (ignore )/
Garbagecollector GB; this definition calls the destructor of garbagecollector when the program exits, crtdbg. in CPP, global variables are defined to manage the memory allocated in our program. This way, when garbagecollector analyzes the structure, these global variables are called to determine whether the new memory has been deleted.

 

Do not perform a test:

 

# Include "crtdbg. H"
# Define new (_ T (_ file _), _ line __)

 

Int winapi winmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
// Todo: Place code here.
_ Crtsetdbgflag (on );
Int * P = new int;
Return 0;
}

 

Program output:

Detected memory leaks!
Dumping objects->
D:/eclipse3.4.3/ce_leakdemo/crtdbg. H (95): normal block at 0x00030060, 4 bytes long
Data <>
./Test. cpp (24): normal block at 0x00030100, 4 bytes long
Data <>
Maximum free store memory allocated at a time: 8!

 

 

To debug this class on Windows Mobile, you must set wchar_t as the built-in type to no compilation in the C/C ++ --- language of the Project attribute.

 

Generally

# Include "crtdbg. H"
# Define new (_ T (_ file _), _ line _) must be added to the end of all the include header files, this will not affect the new and delete calls in the header files you contain, but you can do it if you like.

Related Article

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.