EVC Memory Detection

Source: Internet
Author: User

Save the following code as an. h file and call it in the application that you want to detect.

1 /*****************************************************************2 In order to use memory detection, the following three steps are required in the code to be instrumented:3 1. Define _DEBUG #define _DEBUG4 2. Include "Crtdbg.h" #include "crtdbg.h "5 3. Let your first line in the code is: _CrtSetDbgFlag (on);6 ********************************************************************/7 #ifndef _crtdbg_header8 #define_crtdbg_header9 #ifdef _DEBUGTen   One extern "C" voidwinapiv NKDBGPRINTFW (lpcwstr lpszfmt, ...); A structCrtfilename - { -Unsigned Short*_puscrtname; thecrtfilename*_pcrtnext; - }; -   - struct_crtmem + { -crtfilename*_pcrtfilename; + int_icrtline; AUnsignedint_uicrtmemlen; at void*_pcrtmemaddr; -_crtmem*_pcrtnext; - }; -   - void*operator New(unsignedints,unsigned Short* Name,intLine ); -Inlinevoid* __cdecloperator New(unsignedints) in{return::operator New(S, _t (__file__), __line__);} -   to void__cdecloperator Delete(void*pvmem); +   - classGarbagecollector the { *  Public: $ Garbagecollector () {}Panax Notoginseng~garbagecollector (); - }; the #define_CrtSetDbgFlag (Ignore) garbagecollector GB; +_crtmem* _pcrtmemroot =0; Acrtfilename* _pcrtfilenameroot =0; the void*operator New(unsignedints,unsigned Short* Name,intLine ) + { -  void* Retptr =malloc(s); $ if(retptr) ${_crtmem* _crtmemcell = (struct_crtmem*)malloc(sizeof(_crtmem)); -_crtmemcell->_icrtline =Line ; -_crtmemcell->_uicrtmemlen =s; the_CRTMEMCELL-&GT;_PCRTMEMADDR =retptr; -_crtmemcell->_pcrtnext =0; crtfilename*_tmpcrtfilename;Wuyi  for(_tmpcrtfilename =_pcrtfilenameroot; the_tmpcrtfilename && wcscmp (name, _tmpcrtfilename->_puscrtname); -_tmpcrtfilename = _tmpcrtfilename->_pcrtnext) {} Wu   -  if(!_tmpcrtfilename) About{unsigned Short* _crtname = (unsigned Short*)malloc((wcslen (name) +1) *sizeof(unsigned Short)); $wcscpy (_crtname, name); crtfilename* _crtfilename = (structcrtfilename*)malloc(sizeof(Crtfilename)); -_crtfilename->_puscrtname =_crtname; -_crtfilename->_pcrtnext =0;if(!_pcrtfilenameroot) _pcrtfilenameroot =_crtfilename; -  Else{ for(_tmpcrtfilename =_pcrtfilenameroot; A_tmpcrtfilename->_pcrtnext; +_tmpcrtfilename = _tmpcrtfilename->_pcrtnext); the_tmpcrtfilename->_pcrtnext =_crtfilename; - } $_tmpcrtfilename =_crtfilename; the } the_crtmemcell->_pcrtfilename =_tmpcrtfilename; the if(!_pcrtmemroot) {_pcrtmemroot =_crtmemcell;} the Else{_crtmem* _tmpmemptr; for(_tmpmemptr = _pcrtmemroot; _tmpmemptr->_pcrtnext; _tmpmemptr = _tmpmemptr->_pcrtnext); _tmpmemptr->_pcrtnext =_crtmemcell; - } in } the returnretptr; the } About   the void__cdecloperator Delete(void*Pvmem) the{if(PVMEM) the{_crtmem*_tmpmem; + if(Pvmem = = _pcrtmemroot->_pcrtmemaddr) -{_tmpmem = _pcrtmemroot; _pcrtmemroot = _pcrtmemroot->_pcrtnext; Free(_TMPMEM); the }Bayi Else the{ for(_tmpmem = _pcrtmemroot; _tmpmem->_pcrtnext && (_tmpmem->_pcrtnext->_pcrtmemaddr! =pvmem); the_tmpmem = _tmpmem->_pcrtnext); - if(_tmpmem->_pcrtnext) -{_crtmem*_tmpmem2; the_TMPMEM2 = _tmpmem->_pcrtnext; the_tmpmem->_pcrtnext = _tmpmem2->_pcrtnext; the  Free(_TMPMEM2);} the Else -NKDBGPRINTFW (_t ("%s (%i): warning:deletes memory pointer not allocated with new!/n"), _t (__file__), __line__); the } the  Free(PVMEM); the }94 } the   thegarbagecollector::~Garbagecollector () the{if(!_pcrtmemroot) NKDBGPRINTFW (_t ("No memory leaks detected!/n"));98 Else{_crtmem* _tmpmem; NKDBGPRINTFW (_t ("detected memory leaks!/ndumping objects->/n")); About  for(_tmpmem = _pcrtmemroot; _tmpmem; _tmpmem = _tmpmem->_pcrtnext) - {101NKDBGPRINTFW (_t ("%s (%i): normal block at 0x%08X,%i bytes long/n Data <"), _tmpmem->_pcrtfilename->_puscrtname, _tmpmem->_icrtline, _tmpmem->_pcrtmemaddr, _tmpMem->_uicrtmemlen);102  103  for(unsignedinti =0; I < _tmpmem->_uicrtmemlen; i++)104NKDBGPRINTFW (_t ("%c"), *(((Char*) (_TMPMEM-&GT;_PCRTMEMADDR) +i)); NKDBGPRINTFW (_t (">/n")); the  }106 }107crtfilename* _tmpname =_pcrtfilenameroot;108  for(; _tmpname;)109{_pcrtfilenameroot = _tmpname->_pcrtnext; Free(_tmpname->_puscrtname); Free(_tmpname); _tmpname =_pcrtfilenameroot; the }111}#else the #define_CrtSetDbgFlag (Ignore) #endif//DEBUG #endif//header

EVC Memory Detection

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.