| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> qian tan C/C++ nei cun xie lou ji qi jian ce gong ju

qian tan C/C++ nei cun xie lou ji qi jian ce gong ju

2008-06-01 02:08:14 编辑來源:互联网 评论
 
 
 
本文为【浅谈C/C++内存泄漏及其检测工具】的拼音翻译版
  dui yu yi ge c/c++ cheng xu yuan lai shuo , nei cun xie lou shi yi ge chang jian de ye shi ling ren tou teng de wen ti 。 yi jing you xu duo ji shu bei yan jiu chu lai yi ying dui zhe ge wen ti , bi ru Smart Pointer,Garbage Collection deng 。Smart Pointer ji shu bi jiao cheng shu ,STL zhong yi jing bao han zhi chi Smart Pointer de class, dan shi ta de shi yong si hu bing bu guang fan , er qie ta ye bu neng jie jue suo you de wen ti ;Garbage Collection ji shu zai Java zhong yi jing bi jiao cheng shu , dan shi zai c/c++ ling yu de fa zhan bing bu shun chang , sui ran hen zao jiu you ren si kao zai C++ zhong ye jia ru GC de zhi chi 。 xian shi shi jie jiu shi zhe yang de , zuo wei yi ge c/c++ cheng xu yuan , nei cun xie lou shi ni xin zhong yong yuan de tong 。 bu guo hao zai xian zai you xu duo gong ju neng gou bang zhu wo men yan zheng nei cun xie lou de cun zai , zhao chu fa sheng wen ti de dai ma 。
  nei cun xie lou de ding yi
  yi ban wo men chang shuo de nei cun xie lou shi zhi dui nei cun de xie lou 。 dui nei cun shi zhi cheng xu cong dui zhong fen pei de , da xiao ren yi de ( nei cun kuai de da xiao ke yi zai cheng xu yun xing qi jue ding ), shi yong wan hou bi xu xian shi shi fang de nei cun 。 ying yong cheng xu yi ban shi yong malloc,realloc,new deng han shu cong dui zhong fen pei dao yi kuai nei cun , shi yong wan hou , cheng xu bi xu fu ze xiang ying de tiao yong free huo delete shi fang gai nei cun kuai , fou ze , zhe kuai nei cun jiu bu neng bei zai ci shi yong , wo men jiu shuo zhe kuai nei cun xie lou le 。 yi xia zhe duan xiao cheng xu yan shi le dui nei cun fa sheng xie lou de qing xing :
  void MyFunction(int nSize)
  {
  char* p= new char[nSize];
  if( !GetStringFrom( p, nSize ) ){
  MessageBox(“Error”);
  return;
  }
  …//using the string pointed by p;
  delete p;
  }
  li yi
  dang han shu GetStringFrom() fan hui ling de shi hou , zhi zhen p zhi xiang de nei cun jiu bu hui bei shi fang 。 zhe shi yi zhong chang jian de fa sheng nei cun xie lou de qing xing 。 cheng xu zai ru kou chu fen pei nei cun , zai chu kou chu shi fang nei cun , dan shi c han shu ke yi zai ren he di fang tui chu , suo yi yi dan you mou ge chu kou chu mei you shi fang ying gai shi fang de nei cun , jiu hui fa sheng nei cun xie lou 。
  guang yi de shuo , nei cun xie lou bu jin jin bao han dui nei cun de xie lou , huan bao han xi tong zi yuan de xie lou (resource leak), bi ru gai xin tai HANDLE,GDI Object,SOCKET, Interface deng , cong gen ben shang shuo zhe xie you cao zuo xi tong fen pei de dui xiang ye xiao hao nei cun , jia ru zhe xie dui xiang fa sheng xie lou zui zhong ye hui dao zhi nei cun de xie lou 。 er qie , mou xie dui xiang xiao hao de shi gai xin tai nei cun , zhe xie dui xiang yan chong xie lou shi hui dao zhi zheng ge cao zuo xi tong bu wen ding 。 suo yi xiang bi zhi xia , xi tong zi yuan de xie lou bi dui nei cun de xie lou geng wei yan chong 。
  GDI Object de xie lou shi yi zhong chang jian de zi yuan xie lou :
  void CMyView::OnPaint( CDC* pDC )
  {
  CBitmap bmp;
  CBitmap* pOldBmp;
  bmp.LoadBitmap(IDB_MYBMP);
  pOldBmp = pDC->SelectObject( &bmp );
  …
  if( Something() ){
  return;
  }
  pDC->SelectObject( pOldBmp );
  return;
  }
  li er
  dang han shu Something() fan hui fei ling de shi hou , cheng xu zai tui chu qian mei you ba pOldBmp xuan hui pDC zhong , zhe hui dao zhi pOldBmp zhi xiang de HBITMAP dui xiang fa sheng xie lou 。 zhe ge cheng xu jia ru chang shi jian de yun xing , ke neng hui dao zhi zheng ge xi tong hua ping 。 zhe zhong wen ti zai Win9x xia bi jiao qing yi bao lu chu lai , yin wei Win9x de GDI dui bi Win2k huo NT de yao xiao hen duo 。
  nei cun xie lou de fa sheng fang shi :
  yi fa sheng de fang shi lai fen lei , nei cun xie lou ke yi fen wei 4 lei :
  1. chang fa xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma hui bei duo ci zhi xing dao , mei ci bei zhi xing de shi hou dou hui dao zhi yi kuai nei cun xie lou 。 bi ru li er , jia ru Something() han shu yi zhi fan hui True, na me pOldBmp zhi xiang de HBITMAP dui xiang zong shi fa sheng xie lou 。
  2. ou fa xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma zhi you zai mou xie te ding huan jing huo cao zuo guo cheng xia cai hui fa sheng 。 bi ru li er , jia ru Something() han shu zhi you zai te ding huan jing xia cai fan hui True, na me pOldBmp zhi xiang de HBITMAP dui xiang bing bu zong shi fa sheng xie lou 。 chang fa xing he ou fa xing shi xiang dui de 。 dui yu te ding de huan jing , ou fa xing de ye xu jiu bian cheng le chang fa xing de 。 suo yi ce shi huan jing he ce shi fang fa dui jian ce nei cun xie lou zhi guan chong yao 。
  3. yi ci xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma zhi hui bei zhi xing yi ci , huo zhe you yu suan fa shang de que xian , dao zhi zong hui you yi kuai jin qie yi kuai nei cun fa sheng xie lou 。 bi ru , zai lei de gou zao han shu zhong fen pei nei cun , zai xi gou han shu zhong que mei you shi fang gai nei cun , dan shi yin wei zhe ge lei shi yi ge Singleton, suo yi nei cun xie lou zhi hui fa sheng yi ci 。 ling yi ge li zi :
  char* g_lpszFileName = NULL;
  void SetFileName( const char* lpcszFileName )
  {
  if( g_lpszFileName ){
  free( g_lpszFileName );
  }
  g_lpszFileName = strdup( lpcszFileName );
  }
  li san
  jia ru cheng xu zai jie shu de shi hou mei you shi fang g_lpszFileName zhi xiang de zi fu chuan , na me , ji shi duo ci tiao yong SetFileName(), zong hui you yi kuai nei cun , er qie jin you yi kuai nei cun fa sheng xie lou 。
  4. yin shi nei cun xie lou 。 cheng xu zai yun xing guo cheng zhong bu ting de fen pei nei cun , dan shi zhi dao jie shu de shi hou cai shi fang nei cun 。 yan ge de shuo zhe li bing mei you fa sheng nei cun xie lou , yin wei zui zhong cheng xu shi fang le suo you shen qing de nei cun 。 dan shi dui yu yi ge fu wu qi cheng xu , xu yao yun xing ji tian , ji zhou shen zhi ji ge yue , bu ji shi shi fang nei cun ye ke neng dao zhi zui zhong hao jin xi tong de suo you nei cun 。 suo yi , wo men cheng zhe lei nei cun xie lou wei yin shi nei cun xie lou 。 ju yi ge li zi :
  class Connection
  {
  public:
  Connection( SOCKET s);
  ~Connection();
  …
  private:
  SOCKET _socket;
  …
  };
  class ConnectionManager
  {
  public:
  
   ConnectionManager(){}
  ~ConnectionManager(){
  list::iterator it;
  for( it = _connlist.begin(); it != _connlist.end(); ++it ){
  delete (*it);
  }
  _connlist.clear();
  }
  void OnClientConnected( SOCKET s ){
  Connection* p = new Connection(s);
  _connlist.push_back(p);
  }
  void OnClientDisconnected( Connection* pconn ){
  _connlist.remove( pconn );
  delete pconn;
  }
  private:
  list _connlist;
  };
  li si
  jia she zai Client cong Server duan duan kai hou ,Server bing mei you hu jiao OnClientDisconnected() han shu , na me dai biao na ci lian jie de Connection dui xiang jiu bu hui bei ji shi de shan chu ( zai Server cheng xu tui chu de shi hou , suo you Connection dui xiang hui zai ConnectionManager de xi gou han shu li bei shan chu )。 dang bu duan de you lian jie jian li 、 duan kai shi yin shi nei cun xie lou jiu fa sheng le 。
  cong yong hu shi yong cheng xu de jiao du lai kan , nei cun xie lou ben shen bu hui chan sheng shen me wei hai , zuo wei yi ban de yong hu , gen ben gan jiao bu dao nei cun xie lou de cun zai 。 zhen zheng you wei hai de shi nei cun xie lou de dui ji , zhe hui zui zhong xiao hao jin xi tong suo you de nei cun 。 cong zhe ge jiao du lai shuo , yi ci xing nei cun xie lou bing mei you shen me wei hai , yin wei ta bu hui dui ji , er yin shi nei cun xie lou wei hai xing ze fei chang da , yin wei jiao zhi yu chang fa xing he ou fa xing nei cun xie lou ta geng nan bei jian ce dao 。 Photoshop jiao cheng shu ju jie gou wu bi shu ru fa zhuan ti QQ bing du zhuan ti gong xiang shang wang zhuan ti Google gong ju he fu wu zhuan ti jian ce nei cun xie lou
  jian ce nei cun xie lou de yao hai shi yao neng jie huo zhu dui fen pei nei cun he shi fang nei cun de han shu de tiao yong 。 jie huo zhu zhe liang ge han shu , wo men jiu neng gen zong mei yi kuai nei cun de sheng ming zhou qi , bi ru , mei dang cheng gong de fen pei yi kuai nei cun hou , jiu ba ta de zhi zhen jia ru yi ge quan ju de list zhong ; mei dang shi fang yi kuai nei cun , zai ba ta de zhi zhen cong list zhong shan chu 。 zhe yang , dang cheng xu jie shu de shi hou ,list zhong sheng yu de zhi zhen jiu shi zhi xiang na xie mei you bei shi fang de nei cun 。 zhe li zhi shi jian dan de miao shu le jian ce nei cun xie lou de ji ben yuan li , ju ti de suan fa ke yi shen jian Steve Maguire de <<Writing Solid Code>>。
  jia ru yao jian ce dui nei cun de xie lou , na me xu yao jie huo zhu malloc/realloc/free he new/delete jiu ke yi le ( qi shi new/delete zui zhong ye shi yong malloc/free de , suo yi zhi yao jie huo qian mian yi zu ji ke )。 dui yu qi ta de xie lou , ke yi cai yong lei si de fang fa , jie huo zhu xiang ying de fen pei he shi fang han shu 。 bi ru , yao jian ce BSTR de xie lou , jiu xu yao jie huo SysAllocString/SysFreeString; yao jian ce HMENU de xie lou , jiu xu yao jie huo CreateMenu/ DestroyMenu。( you de zi yuan de fen pei han shu you duo ge , shi fang han shu zhi you yi ge , bi ru ,SysAllocStringLen ye ke yi yong lai fen pei BSTR, zhe shi jiu xu yao jie huo duo ge fen pei han shu )
  zai Windows ping tai xia , jian ce nei cun xie lou de gong ju chang yong de yi ban you san zhong ,MS C-Runtime Library nei jian de jian ce gong neng ; wai gua shi de jian ce gong ju , zhu ru ,Purify,BoundsChecker deng ; li yong Windows NT zi dai de Performance Monitor。 zhe san zhong gong ju ge you you que dian ,MS C-Runtime Library sui ran gong neng shang jiao zhi wai gua shi de gong ju yao ruo , dan shi ta shi mian fei de ;Performance Monitor sui ran wu fa biao shi chu fa sheng wen ti de dai ma , dan shi ta neng jian ce chu yin shi de nei cun xie lou de cun zai , zhe shi qi ta liang lei gong ju wu neng wei li de di fang 。
  yi xia wo men ju ti tao lun zhe san zhong jian ce gong ju :
  VC xia nei cun xie lou de jian ce fang fa
  yong MFC kai fa de ying yong cheng xu , zai DEBUG ban mo shi xia bian yi hou , dou hui zi dong jia ru nei cun xie lou de jian ce dai ma 。 zai cheng xu jie shu hou , jia ru fa sheng le nei cun xie lou , zai Debug chuang kou zhong hui xian shi chu suo you fa sheng xie lou de nei cun kuai de xin xi , yi xia liang xing xian shi le yi kuai bei xie lou de nei cun kuai de xin xi :
  E:\TestMemLeak\TestDlg.cpp(70) : {59} normal block at 0x00881710, 200 bytes long.
  Data: <abcdefghijklmnop> 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70
  di yi xing xian shi gai nei cun kuai you TestDlg.cpp wen jian , di 70 xing dai ma fen pei , di zhi zai 0x00881710, da xiao wei 200 zi jie ,{59} shi zhi tiao yong nei cun fen pei han shu de Request Order, guan yu ta de ju ti xin xi ke yi shen jian MSDN zhong _CrtSetBreakAlloc() de bang zhu 。 di er xing xian shi gai nei cun kuai qian 16 ge zi jie de nei rong , jian kuo hao nei shi yi ASCII fang shi xian shi , jie zhe de shi yi 16 jin zhi fang shi xian shi 。
  
  
  yi ban da jia dou wu yi wei zhe xie nei cun xie lou de jian ce gong neng shi you MFC ti gong de , qi shi bu ran 。MFC zhi shi feng zhuang he li yong le MS C-Runtime Library de Debug Function。 fei MFC cheng xu ye ke yi li yong MS C-Runtime Library de Debug Function jia ru nei cun xie lou de jian ce gong neng 。MS C-Runtime Library zai shi xian malloc/free,strdup deng han shu shi yi jing nei jian le nei cun xie lou de jian ce gong neng 。
  zhu chong guan cha yi xia you MFC Application Wizard sheng cheng de xiang mu , zai mei yi ge cpp wen jian de tou bu dou you zhe yang yi duan hong ding yi :
  #ifdef _DEBUG
  #define new DEBUG_NEW
  #undef THIS_FILE
  static char THIS_FILE[] = __FILE__;
  #endif
  you le zhe yang de ding yi , zai bian yi DEBUG ban shi , chu xian zai zhe ge cpp wen jian zhong de suo you new dou bei ti huan cheng DEBUG_NEW le 。 na me DEBUG_NEW shi shen me ne ?DEBUG_NEW ye shi yi ge hong , yi xia zhai zi afx.h,1632 xing
  #define DEBUG_NEW new(THIS_FILE, __LINE__)
  suo yi jia ru you zhe yang yi xing dai ma :
  char* p = new char[200];
  jing guo hong ti huan jiu bian cheng le :
  char* p = new( THIS_FILE, __LINE__)char[200];
  gen ju C++ de biao zhun , dui yu yi shang de new de shi yong fang fa , bian yi qi hui qu zhao zhe yang ding yi de operator new:
  void* operator new(size_t, LPCSTR, int)
  wo men zai afxmem.cpp 63 xing zhao dao le yi ge zhe yang de operator new de shi xian
  void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
  {
  return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
  }
  void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
  {
  …
  pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);
  if (pResult != NULL)
  return pResult;
  …
  }
  di er ge operator new han shu bi jiao chang , wei le jian dan qi jian , wo zhi zhai lu le bu fen 。 hen xian ran zui hou de nei cun fen pei huan shi tong guo _malloc_dbg han shu shi xian de , zhe ge han shu shu yu MS C-Runtime Library de Debug Function。 zhe ge han shu bu dan yao qiu chuan ru nei cun de da xiao , ling wai huan you wen jian ming he xing hao liang ge shen shu 。 wen jian ming he xing hao jiu shi yong lai ji lu ci ci fen pei shi you na yi duan dai ma zao cheng de 。 jia ru zhe kuai nei cun zai cheng xu jie shu zhi qian mei you bei shi fang , na me zhe xie xin xi jiu hui shu chu dao Debug chuang kou li 。
  zhe li shun bian ti yi xia THIS_FILE,__FILE he __LINE__。__FILE__ he __LINE__ dou shi bian yi qi ding yi de hong 。 dang peng dao __FILE__ shi , bian yi qi hui ba __FILE__ ti huan cheng yi ge zi fu chuan , zhe ge zi fu chuan jiu shi dang qian zai bian yi de wen jian de lu jing ming 。 dang peng dao __LINE__ shi , bian yi qi hui ba __LINE__ ti huan cheng yi ge shu zi , zhe ge shu zi jiu shi dang qian zhe xing dai ma de xing hao 。 zai DEBUG_NEW de ding yi zhong mei you zhi jie shi yong __FILE__, er shi yong le THIS_FILE, qi mu de shi wei le jian xiao mu biao wen jian de da xiao 。 jia she zai mou ge cpp wen jian zhong you 100 chu shi yong le new, jia ru zhi jie shi yong __FILE__, na bian yi qi hui chan sheng 100 ge chang liang zi fu chuan , zhe 100 ge zi fu chuan dou shi sun ?/SPAN>cpp wen jian de lu jing ming , xian ran shi fen rong yu 。 jia ru shi yong THIS_FILE, bian yi qi zhi hui chan sheng yi ge chang liang zi fu chuan , na 100 chu new de tiao yong shi yong de dou shi zhi xiang chang liang zi fu chuan de zhi zhen 。
  zai ci guan cha yi xia you MFC Application Wizard sheng cheng de xiang mu , wo men hui fa xian zai cpp wen jian zhong zhi dui new zuo le yang she , jia ru ni zai cheng xu zhong zhi jie shi yong malloc han shu fen pei nei cun , tiao yong malloc de wen jian ming he xing hao shi bu hui bei ji lu xia lai de 。 jia ru zhe kuai nei cun fa sheng le xie lou ,MS C-Runtime Library reng ran neng jian ce dao , dan shi dang shu chu zhe kuai nei cun kuai de xin xi , bu hui bao han fen pei ta de de wen jian ming he xing hao 。
  yao zai fei MFC cheng xu zhong da kai nei cun xie lou de jian ce gong neng fei chang qing yi , ni zhi yao zai cheng xu de ru kou chu jia ru yi xia ji xing dai ma :
  int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
  tmpFlag = _CRTDBG_LEAK_CHECK_DF;
  _CrtSetDbgFlag( tmpFlag );
  zhe yang , zai cheng xu jie shu de shi hou , ye jiu shi winmain,main huo dllmain han shu fan hui zhi hou , jia ru huan you nei cun kuai mei you shi fang , ta men de xin xi hui bei da yin dao Debug chuang kou li 。
  jia ru ni shi zhe chuang jian le yi ge fei MFC ying yong cheng xu , er qie zai cheng xu de ru kou chu jia ru le yi shang dai ma , bing qie gu yi zai cheng xu zhong bu shi fang mou xie nei cun kuai , ni hui zai Debug chuang kou li kan dao yi xia de xin xi :
  {47} normal block at 0x00C91C90, 200 bytes long.
  Data: < > 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  nei cun xie lou de que jian ce dao le , dan shi he shang mian MFC cheng xu de li zi xiang bi , que shao le wen jian ming he xing hao 。 dui yu yi ge bi jiao da de cheng xu , mei you zhe xie xin xi , jie jue wen ti jiang bian de shi fen kun nan 。
  wei le neng gou zhi dao xie lou de nei cun kuai shi zai na li fen pei de , ni xu yao shi xian lei si MFC de yang she gong neng , ba new,maolloc deng han shu yang she dao _malloc_dbg han shu shang 。 zhe li wo bu zai zhui shu , ni ke yi shen kao MFC de yuan dai ma 。
  you yu Debug Function shi xian zai MS C-RuntimeLibrary zhong , suo yi ta zhi neng jian ce dao dui nei cun de xie lou , er qie zhi xian yu malloc,realloc huo strdup deng fen pei de nei cun , er na xie xi tong zi yuan , bi ru HANDLE,GDI Object, huo shi bu tong guo C-Runtime Library fen pei de nei cun , bi ru VARIANT,BSTR de xie lou , ta shi wu fa jian ce dao de , zhe shi zhe zhong jian ce fa de yi ge chong da de ju xian xing 。 ling wai , wei le neng ji lu nei cun kuai shi zai na li fen pei de , yuan dai ma bi xu xiang ying de pei he , zhe zai tiao shi yi xie lao de cheng xu fei chang ma fan , jiu jing xiu gai yuan dai ma bu shi yi jian sheng xin de shi , zhe shi zhe zhong jian ce fa de ling yi ge ju xian xing 。
  dui yu kai fa yi ge da xing de cheng xu ,MS C-Runtime Library ti gong de jian ce gong neng shi yuan yuan bu gou de 。 jie xia lai wo men jiu kan kan wai gua shi de jian ce gong ju 。 wo yong de bi jiao duo de shi BoundsChecker, yi ze yin wei ta de gong neng bi jiao quan mian , geng chong yao de shi ta de wen ding xing 。 zhe lei gong ju jia ru bu wen ding , fan er hui mang li tian luan 。 dao di shi chu zi ding ding da ming de NuMega, wo yong xia lai ji ben shang mei you shen me da wen ti 。 Photoshop jiao cheng shu ju jie gou wu bi shu ru fa zhuan ti QQ bing du zhuan ti gong xiang shang wang zhuan ti Google gong ju he fu wu zhuan ti
  
   shi yong BoundsChecker jian ce nei cun xie lou :
  BoundsChecker cai yong yi zhong bei cheng wei Code Injection de ji shu , lai jie huo dui fen pei nei cun he shi fang nei cun de han shu de tiao yong 。 jian dan di shuo , dang ni de cheng xu kai shi yun xing shi ,BoundsChecker de DLL bei zi dong zai ru jin cheng de di zhi kong jian ( zhe ke yi tong guo system-level de Hook shi xian ), ran hou ta hui xiu gai jin cheng zhong dui nei cun fen pei he shi fang de han shu tiao yong , rang zhe xie tiao yong shou xian zhuan ru ta de dai ma , ran hou zai zhi xing yuan lai de dai ma 。BoundsChecker zai zuo zhe xie dong zuo de shi , wu xu xiu gai bei tiao shi cheng xu de yuan dai ma huo gong cheng pei zhi wen jian , zhe shi de shi yong ta fei chang de jian bian 、 zhi jie 。
  zhe li wo men yi malloc han shu wei li , jie huo qi ta de han shu fang fa yu ci lei si 。
  xu yao bei jie huo de han shu ke neng zai DLL zhong , ye ke neng zai cheng xu de dai ma li 。 bi ru , jia ru jing tai lian jie C-Runtime Library, na me malloc han shu de dai ma hui bei lian jie dao cheng xu li 。 wei le jie huo zhu dui zhe lei han shu de tiao yong ,BoundsChecker hui dong tai xiu gai zhe xie han shu de zhi ling 。
  yi xia liang duan hui bian dai ma , yi duan mei you BoundsChecker jie ru , ling yi duan ze you BoundsChecker de jie ru :
  126: _CRTIMP void * __cdecl malloc (
  127: size_t nSize
  128: )
  129: {
  00403C10 push ebp
  00403C11 mov ebp,esp
  130: return _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);
  00403C13 push 0
  00403C15 push 0
  00403C17 push 1
  00403C19 mov eax,[__newmode (0042376c)]
  00403C1E push eax
  00403C1F mov ecx,dWord ptr [nSize]
  00403C22 push ecx
  00403C23 call _nh_malloc_dbg (00403c80)
  00403C28 add esp,14h
  131: }
  yi xia zhe yi duan dai ma you BoundsChecker jie ru :
  126: _CRTIMP void * __cdecl malloc (
  127: size_t nSize
  128: )
  129: {
  00403C10 jmp 01F41EC8
  00403C15 push 0
  00403C17 push 1
  00403C19 mov eax,[__newmode (0042376c)]
  00403C1E push eax
  00403C1F mov ecx,dword ptr [nSize]
  00403C22 push ecx
  00403C23 call _nh_malloc_dbg (00403c80)
  00403C28 add esp,14h
  131: }
  dang BoundsChecker jie ru hou , han shu malloc de qian san tiao hui bian zhi ling bei ti huan cheng yi tiao jmp zhi ling , yuan lai de san tiao zhi ling bei ban dao di zhi 01F41EC8 chu le 。 dang cheng xu jin ru malloc hou xian jmp dao 01F41EC8, zhi xing yuan lai de san tiao zhi ling , ran hou jiu shi BoundsChecker de tian xia le 。 da zhi shang ta hui xian ji lu han shu de fan hui di zhi ( han shu de fan hui di zhi zai stack shang , suo yi hen qing yi xiu gai ), ran hou ba fan hui di zhi zhi xiang shu yu BoundsChecker de dai ma , jie zhe tiao dao malloc han shu yuan lai de zhi ling , ye jiu shi zai 00403c15 de di fang 。 dang malloc han shu jie shu de shi hou , you yu fan hui di zhi bei xiu gai , ta hui fan hui dao BoundsChecker de dai ma zhong , ci shi BoundsChecker hui ji lu you malloc fen pei de nei cun de zhi zhen , ran hou zai tiao zhuan dao dao yuan lai de fan hui di zhi qu 。
  jia ru nei cun fen pei / shi fang han shu zai DLL zhong ,BoundsChecker ze cai yong ling yi zhong fang fa lai jie huo dui zhe xie han shu de tiao yong 。BoundsChecker tong guo xiu gai cheng xu de DLL Import Table rang table zhong de han shu di zhi zhi xiang zi ji de di zhi , yi da dao jie huo de mu de 。
  jie huo zhu zhe xie fen pei he shi fang han shu ,BoundsChecker jiu neng ji lu bei fen pei de nei cun huo zi yuan de sheng ming zhou qi 。 jie xia lai de wen ti shi ru he yu yuan dai ma xiang guan , ye jiu shi shuo dang BoundsChecker jian ce dao nei cun xie lou , ta ru he bao gao zhe kuai nei cun kuai shi na duan dai ma fen pei de 。 da an shi tiao shi xin xi (Debug Information)。 dang wo men bian yi yi ge Debug ban de cheng xu shi , bian yi qi hui ba yuan dai ma he er jin zhi dai ma zhi jian de dui ying guan xi ji lu xia lai , fang dao yi ge dan du de wen jian li (.pdb) huo zhe zhi jie lian jie jin mu biao cheng xu , tong guo zhi jie du qu tiao shi xin xi jiu neng de dao fen pei mou kuai nei cun de yuan dai ma zai na ge wen jian , na yi xing shang 。 shi yong Code Injection he Debug Information, shi BoundsChecker bu dan neng ji lu hu jiao fen pei han shu de yuan dai ma de wei zhi , er qie huan neng ji lu fen pei shi de Call Stack, yi ji Call Stack shang de han shu de yuan dai ma wei zhi 。 zhe zai shi yong xiang MFC zhe yang de lei ku shi fei chang you yong , yi xia wo yong yi ge li zi lai shuo ming :
  void ShowXItemMenu()
  {
  …
  CMenu menu;
  menu.CreatePopupMenu();
  //add menu items.
  menu.TrackPropupMenu();
  …
  }
  void ShowYItemMenu( )
  {
  …
  CMenu menu;
  menu.CreatePopupMenu();
  //add menu items.
  menu.TrackPropupMenu();
  menu.Detach();//this will cause HMENU leak
  …
  }
  BOOL CMenu::CreatePopupMenu()
  {
  …
  hMenu = CreatePopupMenu();
  …
  }
  dang tiao yong ShowYItemMenu() shi , wo men gu yi zao cheng HMENU de xie lou 。 dan shi , dui yu BoundsChecker lai shuo bei xie lou de HMENU shi zai class CMenu::CreatePopupMenu() zhong fen pei de 。 jia she de ni de cheng xu you xu duo di fang shi yong le CMenu de CreatePopupMenu() han shu , ru CMenu::CreatePopupMenu() zao cheng de , ni yi ran wu fa que ren wen ti de gen jie dao di zai na li , zai ShowXItemMenu() zhong huan shi zai ShowYItemMenu() zhong , huo zhe huan you qi ta de di fang ye shi yong le CreatePopupMenu()? you le Call Stack de xin xi , wen ti jiu qing yi le 。BoundsChecker hui ru xia bao gao xie lou de HMENU de xin xi :
  
  
  Function
  File
  Line
  CMenu::CreatePopupMenu
  E:\8168\vc98\mfc\mfc\include\afxwin1.inl
  1009
  ShowYItemMenu
  E:\testmemleak\mytest.cpp
  100
  zhe li sheng lue le qi ta de han shu tiao yong
  ru ci , wo men hen qing yi zhao dao fa sheng wen ti de han shu shi ShowYItemMenu()。 dang shi yong MFC zhi lei de lei ku bian cheng shi , da bu fen de API tiao yong dou bei feng zhuang zai lei ku de class li , you le Call Stack xin xi , wo men jiu ke yi fei chang qing yi de zhui zong dao zhen zheng fa sheng xie lou de dai ma 。
  ji lu Call Stack xin xi hui shi cheng xu de yun xing bian de fei chang man , yin ci mo ren qing kuang xia BoundsChecker bu hui ji lu Call Stack xin xi 。 ke yi an zhao yi xia de bu zhou da kai ji lu Call Stack xin xi de xuan xiang kai guan :
  1. da kai cai dan :BoundsCheckerSetting…
  2. zai Error Detection ye zhong , zai Error Detection Scheme de List zhong xuan ze Custom
  3. zai Category de Combox zhong xuan ze Pointer and leak error check
  4. gou shang Report Call Stack fu xuan kuang
  5. dian ji Ok
  ji yu Code Injection,BoundsChecker huan ti gong le API Parameter de xiao yan gong neng ,memory over run deng gong neng 。 zhe xie gong neng dui yu cheng xu de kai fa dou fei chang you yi 。 you yu zhe xie nei rong bu shu yu ben wen de zhu ti , suo yi bu zai ci xiang shu le 。
  jin guan BoundsChecker de gong neng ru ci qiang da , dan shi mian dui yin shi nei cun xie lou reng ran xian de cang bai wu li 。 suo yi jie xia lai wo men kan kan ru he yong Performance Monitor jian ce nei cun xie lou 。
  shi yong Performance Monitor jian ce nei cun xie lou
  NT de nei gai zai she ji guo cheng zhong yi jing jia ru le xi tong jian shi gong neng , bi ru CPU de shi yong lv , nei cun de shi yong qing kuang ,I/O cao zuo de pin fan du deng dou zuo wei yi ge ge Counter, ying yong cheng xu ke yi tong guo du qu zhe xie Counter le jie zheng ge xi tong de huo zhe mou ge jin cheng de yun xing zhuang kuang 。Performance Monitor jiu shi zhe yang yi ge ying yong cheng xu 。
  wei le jian ce nei cun xie lou , wo men yi ban ke yi jian shi Process dui xiang de Handle Count,Virutal Bytes he Working Set san ge Counter。Handle Count ji lu le jin cheng dang qian da kai de HANDLE de ge shu , jian shi zhe ge Counter you zhu yu wo men fa xian cheng xu shi fou you Handle xie lou ;Virtual Bytes ji lu le gai jin cheng dang qian zai xu di zhi kong jian shang shi yong de xu ni nei cun de da xiao ,NT de nei cun fen pei cai yong le liang bu zou de fang fa , shou xian , zai xu di zhi kong jian shang bao liu yi duan kong jian , zhe shi cao zuo xi tong bing mei you fen pei wu li nei cun , zhi shi bao liu le yi duan di zhi 。 ran hou , zai ti jiao zhe duan kong jian , zhe shi cao zuo xi tong cai hui fen pei wu li nei cun 。 suo yi ,Virtual Bytes yi ban zong da yu cheng xu de Working Set。 jian shi Virutal Bytes ke yi bang zhu wo men fa xian yi xie xi tong di ceng de wen ti ; Working Set ji lu le cao zuo xi tong wei jin cheng yi ti jiao de nei cun de zong liang , zhe ge zhi he cheng xu shen qing de nei cun zong liang cun zai mi qie de guan xi , jia ru cheng xu cun zai nei cun de xie lou zhe ge zhi hui chi xu zeng jia , dan shi Virtual Bytes que shi tiao yue shi zeng jia de 。
  jian shi zhe xie Counter ke yi rang wo men le jie jin cheng shi yong nei cun de qing kuang , jia ru fa sheng le xie lou , ji shi shi yin shi nei cun xie lou , zhe xie Counter de zhi ye hui chi xu zeng jia 。 dan shi , wo men zhi dao you wen ti que bu zhi dao na li you wen ti , suo yi yi ban shi yong Performance Monitor lai yan zheng shi fou you nei cun xie lou , er shi yong BoundsChecker lai zhao dao he jie jue 。
  dang Performance Monitor xian shi you nei cun xie lou , er BoundsChecker que wu fa jian ce dao , zhe shi you liang zhong ke neng : di yi zhong , fa sheng le ou fa xing nei cun xie lou 。 zhe shi ni yao que bao shi yong Performance Monitor he shi yong BoundsChecker shi , cheng xu de yun xing huan jing he cao zuo fang fa shi yi zhi de 。 di er zhong , fa sheng le yin shi de nei cun xie lou 。 zhe shi ni yao chong xin shen cha cheng xu de she ji , ran hou zai xi yan jiu Performance Monitor ji lu de Counter de zhi de bian hua tu , fen xi qi zhong de bian hua he cheng xu yun xing luo ji de guan xi , zhao dao yi xie ke neng de yuan yin 。 zhe shi yi ge tong ku de guo cheng , bu man le jia she 、 cai xiang 、 yan zheng 、 shi bai , dan zhe ye shi yi ge ji lei jing yan de jue hao ji hui 。
  zong jie
  nei cun xie lou shi ge da er fu duo de wen ti , ji shi shi Java he .Net zhe yang you Gabarge Collection ji zhi de huan jing , ye cun zai zhe xie lou de ke neng , bi ru yin shi nei cun xie lou 。 you yu pian fu he neng li de xian zhi , ben wen zhi neng dui zhe ge zhu ti zuo yi ge cu qian de yan jiu 。 qi ta de wen ti , bi ru duo mo kuai xia de xie lou jian ce , ru he zai cheng xu yun xing shi dui nei cun shi yong qing kuang jin xing fen xi deng deng , dou shi ke yi shen ru yan jiu de ti mu 。 jia ru nin you shen me xiang fa , jian yi huo fa xian le mou xie cuo wu , huan ying he wo jiao liu 。【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
   dui yu yi ge c/c++ cheng xu yuan lai shuo , nei cun xie lou shi yi ge chang jian de ye shi ling ren tou teng de wen ti 。 yi jing you xu duo ji shu bei yan jiu chu lai yi ying dui zhe ge wen ti , bi ru Smart Pointer,Garbage Collection deng 。Smart Pointer ji shu bi jiao cheng shu ,STL zhong yi jing bao han zhi chi Smart Pointer de class, dan shi ta de shi yong si hu bing bu guang fan , er qie ta ye bu neng jie jue suo you de wen ti ;Garbage Collection ji shu zai Java zhong yi jing bi jiao cheng shu , dan shi zai c/c++ ling yu de fa zhan bing bu shun chang , sui ran hen zao jiu you ren si kao zai C++ zhong ye jia ru GC de zhi chi 。 xian shi shi jie jiu shi zhe yang de , zuo wei yi ge c/c++ cheng xu yuan , nei cun xie lou shi ni xin zhong yong yuan de tong 。 bu guo hao zai xian zai you xu duo gong ju neng gou bang zhu wo men yan zheng nei cun xie lou de cun zai , zhao chu fa sheng wen ti de dai ma 。    nei cun xie lou de ding yi    yi ban wo men chang shuo de nei cun xie lou shi zhi dui nei cun de xie lou 。 dui nei cun shi zhi cheng xu cong dui zhong fen pei de , da xiao ren yi de ( nei cun kuai de da xiao ke yi zai cheng xu yun xing qi jue ding ), shi yong wan hou bi xu xian shi shi fang de nei cun 。 ying yong cheng xu yi ban shi yong malloc,realloc,new deng han shu cong dui zhong fen pei dao yi kuai nei cun , shi yong wan hou , cheng xu bi xu fu ze xiang ying de tiao yong free huo delete shi fang gai nei cun kuai , fou ze , zhe kuai nei cun jiu bu neng bei zai ci shi yong , wo men jiu shuo zhe kuai nei cun xie lou le 。 yi xia zhe duan xiao cheng xu yan shi le dui nei cun fa sheng xie lou de qing xing : void MyFunction(int nSize) {  char* p= new char[nSize];  if( !GetStringFrom( p, nSize ) ){   MessageBox(“Error”);   return;  }  …//using the string pointed by p;  delete p; }    li yi    dang han shu GetStringFrom() fan hui ling de shi hou , zhi zhen p zhi xiang de nei cun jiu bu hui bei shi fang 。 zhe shi yi zhong chang jian de fa sheng nei cun xie lou de qing xing 。 cheng xu zai ru kou chu fen pei nei cun , zai chu kou chu shi fang nei cun , dan shi c han shu ke yi zai ren he di fang tui chu , suo yi yi dan you mou ge chu kou chu mei you shi fang ying gai shi fang de nei cun , jiu hui fa sheng nei cun xie lou 。    guang yi de shuo , nei cun xie lou bu jin jin bao han dui nei cun de xie lou , huan bao han xi tong zi yuan de xie lou (resource leak), bi ru gai xin tai HANDLE,GDI Object,SOCKET, Interface deng , cong gen ben shang shuo zhe xie you cao zuo xi tong fen pei de dui xiang ye xiao hao nei cun , jia ru zhe xie dui xiang fa sheng xie lou zui zhong ye hui dao zhi nei cun de xie lou 。 er qie , mou xie dui xiang xiao hao de shi gai xin tai nei cun , zhe xie dui xiang yan chong xie lou shi hui dao zhi zheng ge cao zuo xi tong bu wen ding 。 suo yi xiang bi zhi xia , xi tong zi yuan de xie lou bi dui nei cun de xie lou geng wei yan chong 。   GDI Object de xie lou shi yi zhong chang jian de zi yuan xie lou : void CMyView::OnPaint( CDC* pDC ) {  CBitmap bmp;  CBitmap* pOldBmp;  bmp.LoadBitmap(IDB_MYBMP);  pOldBmp = pDC->SelectObject( &bmp );  …  if( Something() ){   return;  }  pDC->SelectObject( pOldBmp );  return; }    li er    dang han shu Something() fan hui fei ling de shi hou , cheng xu zai tui chu qian mei you ba pOldBmp xuan hui pDC zhong , zhe hui dao zhi pOldBmp zhi xiang de HBITMAP dui xiang fa sheng xie lou 。 zhe ge cheng xu jia ru chang shi jian de yun xing , ke neng hui dao zhi zheng ge xi tong hua ping 。 zhe zhong wen ti zai Win9x xia bi jiao qing yi bao lu chu lai , yin wei Win9x de GDI dui bi Win2k huo NT de yao xiao hen duo 。    nei cun xie lou de fa sheng fang shi :    yi fa sheng de fang shi lai fen lei , nei cun xie lou ke yi fen wei 4 lei :   1. chang fa xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma hui bei duo ci zhi xing dao , mei ci bei zhi xing de shi hou dou hui dao zhi yi kuai nei cun xie lou 。 bi ru li er , jia ru Something() han shu yi zhi fan hui True, na me pOldBmp zhi xiang de HBITMAP dui xiang zong shi fa sheng xie lou 。   2. ou fa xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma zhi you zai mou xie te ding huan jing huo cao zuo guo cheng xia cai hui fa sheng 。 bi ru li er , jia ru Something() han shu zhi you zai te ding huan jing xia cai fan hui True, na me pOldBmp zhi xiang de HBITMAP dui xiang bing bu zong shi fa sheng xie lou 。 chang fa xing he ou fa xing shi xiang dui de 。 dui yu te ding de huan jing , ou fa xing de ye xu jiu bian cheng le chang fa xing de 。 suo yi ce shi huan jing he ce shi fang fa dui jian ce nei cun xie lou zhi guan chong yao 。   3. yi ci xing nei cun xie lou 。 fa sheng nei cun xie lou de dai ma zhi hui bei zhi xing yi ci , huo zhe you yu suan fa shang de que xian , dao zhi zong hui you yi kuai jin qie yi kuai nei cun fa sheng xie lou 。 bi ru , zai lei de gou zao han shu zhong fen pei nei cun , zai xi gou han shu zhong que mei you shi fang gai nei cun , dan shi yin wei zhe ge lei shi yi ge Singleton, suo yi nei cun xie lou zhi hui fa sheng yi ci 。 ling yi ge li zi : char* g_lpszFileName = NULL; void SetFileName( const char* lpcszFileName ) {  if( g_lpszFileName ){   free( g_lpszFileName );  }  g_lpszFileName = strdup( lpcszFileName ); }    li san    jia ru cheng xu zai jie shu de shi hou mei you shi fang g_lpszFileName zhi xiang de zi fu chuan , na me , ji shi duo ci tiao yong SetFileName(), zong hui you yi kuai nei cun , er qie jin you yi kuai nei cun fa sheng xie lou 。   4. yin shi nei cun xie lou 。 cheng xu zai yun xing guo cheng zhong bu ting de fen pei nei cun , dan shi zhi dao jie shu de shi hou cai shi fang nei cun 。 yan ge de shuo zhe li bing mei you fa sheng nei cun xie lou , yin wei zui zhong cheng xu shi fang le suo you shen qing de nei cun 。 dan shi dui yu yi ge fu wu qi cheng xu , xu yao yun xing ji tian , ji zhou shen zhi ji ge yue , bu ji shi shi fang nei cun ye ke neng dao zhi zui zhong hao jin xi tong de suo you nei cun 。 suo yi , wo men cheng zhe lei nei cun xie lou wei yin shi nei cun xie lou 。 ju yi ge li zi : class Connection {  public:   Connection( SOCKET s);   ~Connection();   …  private:   SOCKET _socket;   … }; class ConnectionManager {  public:   ConnectionManager(){}   ~ConnectionManager(){    list::iterator it;    for( it = _connlist.begin(); it != _connlist.end(); ++it ){     delete (*it);    }    _connlist.clear();   }   void OnClientConnected( SOCKET s ){    Connection* p = new Connection(s);    _connlist.push_back(p);   }   void OnClientDisconnected( Connection* pconn ){    _connlist.remove( pconn );    delete pconn;   }  private:   list _connlist; };    li si    jia she zai Client cong Server duan duan kai hou ,Server bing mei you hu jiao OnClientDisconnected() han shu , na me dai biao na ci lian jie de Connection dui xiang jiu bu hui bei ji shi de shan chu ( zai Server cheng xu tui chu de shi hou , suo you Connection dui xiang hui zai ConnectionManager de xi gou han shu li bei shan chu )。 dang bu duan de you lian jie jian li 、 duan kai shi yin shi nei cun xie lou jiu fa sheng le 。    cong yong hu shi yong cheng xu de jiao du lai kan , nei cun xie lou ben shen bu hui chan sheng shen me wei hai , zuo wei yi ban de yong hu , gen ben gan jiao bu dao nei cun xie lou de cun zai 。 zhen zheng you wei hai de shi nei cun xie lou de dui ji , zhe hui zui zhong xiao hao jin xi tong suo you de nei cun 。 cong zhe ge jiao du lai shuo , yi ci xing nei cun xie lou bing mei you shen me wei hai , yin wei ta bu hui dui ji , er yin shi nei cun xie lou wei hai xing ze fei chang da , yin wei jiao zhi yu chang fa xing he ou fa xing nei cun xie lou ta geng nan bei jian ce dao 。 Photoshop jiao cheng shu ju jie gou wu bi shu ru fa zhuan ti QQ bing du zhuan ti gong xiang shang wang zhuan ti Google gong ju he fu wu zhuan ti jian ce nei cun xie lou    jian ce nei cun xie lou de yao hai shi yao neng jie huo zhu dui fen pei nei cun he shi fang nei cun de han shu de tiao yong 。 jie huo zhu zhe liang ge han shu , wo men jiu neng gen zong mei yi kuai nei cun de sheng ming zhou qi , bi ru , mei dang cheng gong de fen pei yi kuai nei cun hou , jiu ba ta de zhi zhen jia ru yi ge quan ju de list zhong ; mei dang shi fang yi kuai nei cun , zai ba ta de zhi zhen cong list zhong shan chu 。 zhe yang , dang cheng xu jie shu de shi hou ,list zhong sheng yu de zhi zhen jiu shi zhi xiang na xie mei you bei shi fang de nei cun 。 zhe li zhi shi jian dan de miao shu le jian ce nei cun xie lou de ji ben yuan li , ju ti de suan fa ke yi shen jian Steve Maguire de <<Writing Solid Code>>。    jia ru yao jian ce dui nei cun de xie lou , na me xu yao jie huo zhu malloc/realloc/free he new/delete jiu ke yi le ( qi shi new/delete zui zhong ye shi yong malloc/free de , suo yi zhi yao jie huo qian mian yi zu ji ke )。 dui yu qi ta de xie lou , ke yi cai yong lei si de fang fa , jie huo zhu xiang ying de fen pei he shi fang han shu 。 bi ru , yao jian ce BSTR de xie lou , jiu xu yao jie huo SysAllocString/SysFreeString; yao jian ce HMENU de xie lou , jiu xu yao jie huo CreateMenu/ DestroyMenu。( you de zi yuan de fen pei han shu you duo ge , shi fang han shu zhi you yi ge , bi ru ,SysAllocStringLen ye ke yi yong lai fen pei BSTR, zhe shi jiu xu yao jie huo duo ge fen pei han shu )    zai Windows ping tai xia , jian ce nei cun xie lou de gong ju chang yong de yi ban you san zhong ,MS C-Runtime Library nei jian de jian ce gong neng ; wai gua shi de jian ce gong ju , zhu ru ,Purify,BoundsChecker deng ; li yong Windows NT zi dai de Performance Monitor。 zhe san zhong gong ju ge you you que dian ,MS C-Runtime Library sui ran gong neng shang jiao zhi wai gua shi de gong ju yao ruo , dan shi ta shi mian fei de ;Performance Monitor sui ran wu fa biao shi chu fa sheng wen ti de dai ma , dan shi ta neng jian ce chu yin shi de nei cun xie lou de cun zai , zhe shi qi ta liang lei gong ju wu neng wei li de di fang 。    yi xia wo men ju ti tao lun zhe san zhong jian ce gong ju :   VC xia nei cun xie lou de jian ce fang fa    yong MFC kai fa de ying yong cheng xu , zai DEBUG ban mo shi xia bian yi hou , dou hui zi dong jia ru nei cun xie lou de jian ce dai ma 。 zai cheng xu jie shu hou , jia ru fa sheng le nei cun xie lou , zai Debug chuang kou zhong hui xian shi chu suo you fa sheng xie lou de nei cun kuai de xin xi , yi xia liang xing xian shi le yi kuai bei xie lou de nei cun kuai de xin xi : E:\TestMemLeak\TestDlg.cpp(70) : {59} normal block at 0x00881710, 200 bytes long. Data: <abcdefghijklmnop> 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70    di yi xing xian shi gai nei cun kuai you TestDlg.cpp wen jian , di 70 xing dai ma fen pei , di zhi zai 0x00881710, da xiao wei 200 zi jie ,{59} shi zhi tiao yong nei cun fen pei han shu de Request Order, guan yu ta de ju ti xin xi ke yi shen jian MSDN zhong _CrtSetBreakAlloc() de bang zhu 。 di er xing xian shi gai nei cun kuai qian 16 ge zi jie de nei rong , jian kuo hao nei shi yi ASCII fang shi xian shi , jie zhe de shi yi 16 jin zhi fang shi xian shi 。    yi ban da jia dou wu yi wei zhe xie nei cun xie lou de jian ce gong neng shi you MFC ti gong de , qi shi bu ran 。MFC zhi shi feng zhuang he li yong le MS C-Runtime Library de Debug Function。 fei MFC cheng xu ye ke yi li yong MS C-Runtime Library de Debug Function jia ru nei cun xie lou de jian ce gong neng 。MS C-Runtime Library zai shi xian malloc/free,strdup deng han shu shi yi jing nei jian le nei cun xie lou de jian ce gong neng 。    zhu chong guan cha yi xia you MFC Application Wizard sheng cheng de xiang mu , zai mei yi ge cpp wen jian de tou bu dou you zhe yang yi duan hong ding yi : #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif    you le zhe yang de ding yi , zai bian yi DEBUG ban shi , chu xian zai zhe ge cpp wen jian zhong de suo you new dou bei ti huan cheng DEBUG_NEW le 。 na me DEBUG_NEW shi shen me ne ?DEBUG_NEW ye shi yi ge hong , yi xia zhai zi afx.h,1632 xing #define DEBUG_NEW new(THIS_FILE, __LINE__)    suo yi jia ru you zhe yang yi xing dai ma : char* p = new char[200];    jing guo hong ti huan jiu bian cheng le : char* p = new( THIS_FILE, __LINE__)char[200];    gen ju C++ de biao zhun , dui yu yi shang de new de shi yong fang fa , bian yi qi hui qu zhao zhe yang ding yi de operator new: void* operator new(size_t, LPCSTR, int)    wo men zai afxmem.cpp 63 xing zhao dao le yi ge zhe yang de operator new de shi xian void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine) {  return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine); } void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine) {  …  pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);  if (pResult != NULL)   return pResult;  … }    di er ge operator new han shu bi jiao chang , wei le jian dan qi jian , wo zhi zhai lu le bu fen 。 hen xian ran zui hou de nei cun fen pei huan shi tong guo _malloc_dbg han shu shi xian de , zhe ge han shu shu yu MS C-Runtime Library de Debug Function。 zhe ge han shu bu dan yao qiu chuan ru nei cun de da xiao , ling wai huan you wen jian ming he xing hao liang ge shen shu 。 wen jian ming he xing hao jiu shi yong lai ji lu ci ci fen pei shi you na yi duan dai ma zao cheng de 。 jia ru zhe kuai nei cun zai cheng xu jie shu zhi qian mei you bei shi fang , na me zhe xie xin xi jiu hui shu chu dao Debug chuang kou li 。    zhe li shun bian ti yi xia THIS_FILE,__FILE he __LINE__。__FILE__ he __LINE__ dou shi bian yi qi ding yi de hong 。 dang peng dao __FILE__ shi , bian yi qi hui ba __FILE__ ti huan cheng yi ge zi fu chuan , zhe ge zi fu chuan jiu shi dang qian zai bian yi de wen jian de lu jing ming 。 dang peng dao __LINE__ shi , bian yi qi hui ba __LINE__ ti huan cheng yi ge shu zi , zhe ge shu zi jiu shi dang qian zhe xing dai ma de xing hao 。 zai DEBUG_NEW de ding yi zhong mei you zhi jie shi yong __FILE__, er shi yong le THIS_FILE, qi mu de shi wei le jian xiao mu biao wen jian de da xiao 。 jia she zai mou ge cpp wen jian zhong you 100 chu shi yong le new, jia ru zhi jie shi yong __FILE__, na bian yi qi hui chan sheng 100 ge chang liang zi fu chuan , zhe 100 ge zi fu chuan dou shi sun ?/SPAN>cpp wen jian de lu jing ming , xian ran shi fen rong yu 。 jia ru shi yong THIS_FILE, bian yi qi zhi hui chan sheng yi ge chang liang zi fu chuan , na 100 chu new de tiao yong shi yong de dou shi zhi xiang chang liang zi fu chuan de zhi zhen 。    zai ci guan cha yi xia you MFC Application Wizard sheng cheng de xiang mu , wo men hui fa xian zai cpp wen jian zhong zhi dui new zuo le yang she , jia ru ni zai cheng xu zhong zhi jie shi yong malloc han shu fen pei nei cun , tiao yong malloc de wen jian ming he xing hao shi bu hui bei ji lu xia lai de 。 jia ru zhe kuai nei cun fa sheng le xie lou ,MS C-Runtime Library reng ran neng jian ce dao , dan shi dang shu chu zhe kuai nei cun kuai de xin xi , bu hui bao han fen pei ta de de wen jian ming he xing hao 。    yao zai fei MFC cheng xu zhong da kai nei cun xie lou de jian ce gong neng fei chang qing yi , ni zhi yao zai cheng xu de ru kou chu jia ru yi xia ji xing dai ma : int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); tmpFlag = _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag( tmpFlag );    zhe yang , zai cheng xu jie shu de shi hou , ye jiu shi winmain,main huo dllmain han shu fan hui zhi hou , jia ru huan you nei cun kuai mei you shi fang , ta men de xin xi hui bei da yin dao Debug chuang kou li 。    jia ru ni shi zhe chuang jian le yi ge fei MFC ying yong cheng xu , er qie zai cheng xu de ru kou chu jia ru le yi shang dai ma , bing qie gu yi zai cheng xu zhong bu shi fang mou xie nei cun kuai , ni hui zai Debug chuang kou li kan dao yi xia de xin xi : {47} normal block at 0x00C91C90, 200 bytes long. Data: < > 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F    nei cun xie lou de que jian ce dao le , dan shi he shang mian MFC cheng xu de li zi xiang bi , que shao le wen jian ming he xing hao 。 dui yu yi ge bi jiao da de cheng xu , mei you zhe xie xin xi , jie jue wen ti jiang bian de shi fen kun nan 。    wei le neng gou zhi dao xie lou de nei cun kuai shi zai na li fen pei de , ni xu yao shi xian lei si MFC de yang she gong neng , ba new,maolloc deng han shu yang she dao _malloc_dbg han shu shang 。 zhe li wo bu zai zhui shu , ni ke yi shen kao MFC de yuan dai ma 。    you yu Debug Function shi xian zai MS C-RuntimeLibrary zhong , suo yi ta zhi neng jian ce dao dui nei cun de xie lou , er qie zhi xian yu malloc,realloc huo strdup deng fen pei de nei cun , er na xie xi tong zi yuan , bi ru HANDLE,GDI Object, huo shi bu tong guo C-Runtime Library fen pei de nei cun , bi ru VARIANT,BSTR de xie lou , ta shi wu fa jian ce dao de , zhe shi zhe zhong jian ce fa de yi ge chong da de ju xian xing 。 ling wai , wei le neng ji lu nei cun kuai shi zai na li fen pei de , yuan dai ma bi xu xiang ying de pei he , zhe zai tiao shi yi xie lao de cheng xu fei chang ma fan , jiu jing xiu gai yuan dai ma bu shi yi jian sheng xin de shi , zhe shi zhe zhong jian ce fa de ling yi ge ju xian xing 。    dui yu kai fa yi ge da xing de cheng xu ,MS C-Runtime Library ti gong de jian ce gong neng shi yuan yuan bu gou de 。 jie xia lai wo men jiu kan kan wai gua shi de jian ce gong ju 。 wo yong de bi jiao duo de shi BoundsChecker, yi ze yin wei ta de gong neng bi jiao quan mian , geng chong yao de shi ta de wen ding xing 。 zhe lei gong ju jia ru bu wen ding , fan er hui mang li tian luan 。 dao di shi chu zi ding ding da ming de NuMega, wo yong xia lai ji ben shang mei you shen me da wen ti 。 Photoshop jiao cheng shu ju jie gou wu bi shu ru fa zhuan ti QQ bing du zhuan ti gong xiang shang wang zhuan ti Google gong ju he fu wu zhuan ti    shi yong BoundsChecker jian ce nei cun xie lou :   BoundsChecker cai yong yi zhong bei cheng wei Code Injection de ji shu , lai jie huo dui fen pei nei cun he shi fang nei cun de han shu de tiao yong 。 jian dan di shuo , dang ni de cheng xu kai shi yun xing shi ,BoundsChecker de DLL bei zi dong zai ru jin cheng de di zhi kong jian ( zhe ke yi tong guo system-level de Hook shi xian ), ran hou ta hui xiu gai jin cheng zhong dui nei cun fen pei he shi fang de han shu tiao yong , rang zhe xie tiao yong shou xian zhuan ru ta de dai ma , ran hou zai zhi xing yuan lai de dai ma 。BoundsChecker zai zuo zhe xie dong zuo de shi , wu xu xiu gai bei tiao shi cheng xu de yuan dai ma huo gong cheng pei zhi wen jian , zhe shi de shi yong ta fei chang de jian bian 、 zhi jie 。    zhe li wo men yi malloc han shu wei li , jie huo qi ta de han shu fang fa yu ci lei si 。    xu yao bei jie huo de han shu ke neng zai DLL zhong , ye ke neng zai cheng xu de dai ma li 。 bi ru , jia ru jing tai lian jie C-Runtime Library, na me malloc han shu de dai ma hui bei lian jie dao cheng xu li 。 wei le jie huo zhu dui zhe lei han shu de tiao yong ,BoundsChecker hui dong tai xiu gai zhe xie han shu de zhi ling 。    yi xia liang duan hui bian dai ma , yi duan mei you BoundsChecker jie ru , ling yi duan ze you BoundsChecker de jie ru : 126: _CRTIMP void * __cdecl malloc ( 127: size_t nSize 128: ) 129: { 00403C10 push ebp 00403C11 mov ebp,esp 130: return _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0); 00403C13 push 0 00403C15 push 0 00403C17 push 1 00403C19 mov eax,[__newmode (0042376c)] 00403C1E push eax 00403C1F mov ecx,dWord ptr [nSize] 00403C22 push ecx 00403C23 call _nh_malloc_dbg (00403c80) 00403C28 add esp,14h 131: }    yi xia zhe yi duan dai ma you BoundsChecker jie ru : 126: _CRTIMP void * __cdecl malloc ( 127: size_t nSize 128: ) 129: { 00403C10 jmp 01F41EC8 00403C15 push 0 00403C17 push 1 00403C19 mov eax,[__newmode (0042376c)] 00403C1E push eax 00403C1F mov ecx,dword ptr [nSize] 00403C22 push ecx 00403C23 call _nh_malloc_dbg (00403c80) 00403C28 add esp,14h 131: }    dang BoundsChecker jie ru hou , han shu malloc de qian san tiao hui bian zhi ling bei ti huan cheng yi tiao jmp zhi ling , yuan lai de san tiao zhi ling bei ban dao di zhi 01F41EC8 chu le 。 dang cheng xu jin ru malloc hou xian jmp dao 01F41EC8, zhi xing yuan lai de san tiao zhi ling , ran hou jiu shi BoundsChecker de tian xia le 。 da zhi shang ta hui xian ji lu han shu de fan hui di zhi ( han shu de fan hui di zhi zai stack shang , suo yi hen qing yi xiu gai ), ran hou ba fan hui di zhi zhi xiang shu yu BoundsChecker de dai ma , jie zhe tiao dao malloc han shu yuan lai de zhi ling , ye jiu shi zai 00403c15 de di fang 。 dang malloc han shu jie shu de shi hou , you yu fan hui di zhi bei xiu gai , ta hui fan hui dao BoundsChecker de dai ma zhong , ci shi BoundsChecker hui ji lu you malloc fen pei de nei cun de zhi zhen , ran hou zai tiao zhuan dao dao yuan lai de fan hui di zhi qu 。    jia ru nei cun fen pei / shi fang han shu zai DLL zhong ,BoundsChecker ze cai yong ling yi zhong fang fa lai jie huo dui zhe xie han shu de tiao yong 。BoundsChecker tong guo xiu gai cheng xu de DLL Import Table rang table zhong de han shu di zhi zhi xiang zi ji de di zhi , yi da dao jie huo de mu de 。    jie huo zhu zhe xie fen pei he shi fang han shu ,BoundsChecker jiu neng ji lu bei fen pei de nei cun huo zi yuan de sheng ming zhou qi 。 jie xia lai de wen ti shi ru he yu yuan dai ma xiang guan , ye jiu shi shuo dang BoundsChecker jian ce dao nei cun xie lou , ta ru he bao gao zhe kuai nei cun kuai shi na duan dai ma fen pei de 。 da an shi tiao shi xin xi (Debug Information)。 dang wo men bian yi yi ge Debug ban de cheng xu shi , bian yi qi hui ba yuan dai ma he er jin zhi dai ma zhi jian de dui ying guan xi ji lu xia lai , fang dao yi ge dan du de wen jian li (.pdb) huo zhe zhi jie lian jie jin mu biao cheng xu , tong guo zhi jie du qu tiao shi xin xi jiu neng de dao fen pei mou kuai nei cun de yuan dai ma zai na ge wen jian , na yi xing shang 。 shi yong Code Injection he Debug Information, shi BoundsChecker bu dan neng ji lu hu jiao fen pei han shu de yuan dai ma de wei zhi , er qie huan neng ji lu fen pei shi de Call Stack, yi ji Call Stack shang de han shu de yuan dai ma wei zhi 。 zhe zai shi yong xiang MFC zhe yang de lei ku shi fei chang you yong , yi xia wo yong yi ge li zi lai shuo ming : void ShowXItemMenu() {  …  CMenu menu;  menu.CreatePopupMenu();  //add menu items.  menu.TrackPropupMenu();  … } void ShowYItemMenu( ) {  …  CMenu menu;  menu.CreatePopupMenu();  //add menu items.  menu.TrackPropupMenu();  menu.Detach();//this will cause HMENU leak  … } BOOL CMenu::CreatePopupMenu() {  …  hMenu = CreatePopupMenu();  … }    dang tiao yong ShowYItemMenu() shi , wo men gu yi zao cheng HMENU de xie lou 。 dan shi , dui yu BoundsChecker lai shuo bei xie lou de HMENU shi zai class CMenu::CreatePopupMenu() zhong fen pei de 。 jia she de ni de cheng xu you xu duo di fang shi yong le CMenu de CreatePopupMenu() han shu , ru CMenu::CreatePopupMenu() zao cheng de , ni yi ran wu fa que ren wen ti de gen jie dao di zai na li , zai ShowXItemMenu() zhong huan shi zai ShowYItemMenu() zhong , huo zhe huan you qi ta de di fang ye shi yong le CreatePopupMenu()? you le Call Stack de xin xi , wen ti jiu qing yi le 。BoundsChecker hui ru xia bao gao xie lou de HMENU de xin xi : Function File Line CMenu::CreatePopupMenu E:\8168\vc98\mfc\mfc\include\afxwin1.inl 1009 ShowYItemMenu E:\testmemleak\mytest.cpp 100    zhe li sheng lue le qi ta de han shu tiao yong    ru ci , wo men hen qing yi zhao dao fa sheng wen ti de han shu shi ShowYItemMenu()。 dang shi yong MFC zhi lei de lei ku bian cheng shi , da bu fen de API tiao yong dou bei feng zhuang zai lei ku de class li , you le Call Stack xin xi , wo men jiu ke yi fei chang qing yi de zhui zong dao zhen zheng fa sheng xie lou de dai ma 。    ji lu Call Stack xin xi hui shi cheng xu de yun xing bian de fei chang man , yin ci mo ren qing kuang xia BoundsChecker bu hui ji lu Call Stack xin xi 。 ke yi an zhao yi xia de bu zhou da kai ji lu Call Stack xin xi de xuan xiang kai guan :   1. da kai cai dan :BoundsCheckerSetting…   2. zai Error Detection ye zhong , zai Error Detection Scheme de List zhong xuan ze Custom   3. zai Category de Combox zhong xuan ze Pointer and leak error check   4. gou shang Report Call Stack fu xuan kuang   5. dian ji Ok    ji yu Code Injection,BoundsChecker huan ti gong le API Parameter de xiao yan gong neng ,memory over run deng gong neng 。 zhe xie gong neng dui yu cheng xu de kai fa dou fei chang you yi 。 you yu zhe xie nei rong bu shu yu ben wen de zhu ti , suo yi bu zai ci xiang shu le 。    jin guan BoundsChecker de gong neng ru ci qiang da , dan shi mian dui yin shi nei cun xie lou reng ran xian de cang bai wu li 。 suo yi jie xia lai wo men kan kan ru he yong Performance Monitor jian ce nei cun xie lou 。    shi yong Performance Monitor jian ce nei cun xie lou   NT de nei gai zai she ji guo cheng zhong yi jing jia ru le xi tong jian shi gong neng , bi ru CPU de shi yong lv , nei cun de shi yong qing kuang ,I/O cao zuo de pin fan du deng dou zuo wei yi ge ge Counter, ying yong cheng xu ke yi tong guo du qu zhe xie Counter le jie zheng ge xi tong de huo zhe mou ge jin cheng de yun xing zhuang kuang 。Performance Monitor jiu shi zhe yang yi ge ying yong cheng xu 。    wei le jian ce nei cun xie lou , wo men yi ban ke yi jian shi Process dui xiang de Handle Count,Virutal Bytes he Working Set san ge Counter。Handle Count ji lu le jin cheng dang qian da kai de HANDLE de ge shu , jian shi zhe ge Counter you zhu yu wo men fa xian cheng xu shi fou you Handle xie lou ;Virtual Bytes ji lu le gai jin cheng dang qian zai xu di zhi kong jian shang shi yong de xu ni nei cun de da xiao ,NT de nei cun fen pei cai yong le liang bu zou de fang fa , shou xian , zai xu di zhi kong jian shang bao liu yi duan kong jian , zhe shi cao zuo xi tong bing mei you fen pei wu li nei cun , zhi shi bao liu le yi duan di zhi 。 ran hou , zai ti jiao zhe duan kong jian , zhe shi cao zuo xi tong cai hui fen pei wu li nei cun 。 suo yi ,Virtual Bytes yi ban zong da yu cheng xu de Working Set。 jian shi Virutal Bytes ke yi bang zhu wo men fa xian yi xie xi tong di ceng de wen ti ; Working Set ji lu le cao zuo xi tong wei jin cheng yi ti jiao de nei cun de zong liang , zhe ge zhi he cheng xu shen qing de nei cun zong liang cun zai mi qie de guan xi , jia ru cheng xu cun zai nei cun de xie lou zhe ge zhi hui chi xu zeng jia , dan shi Virtual Bytes que shi tiao yue shi zeng jia de 。    jian shi zhe xie Counter ke yi rang wo men le jie jin cheng shi yong nei cun de qing kuang , jia ru fa sheng le xie lou , ji shi shi yin shi nei cun xie lou , zhe xie Counter de zhi ye hui chi xu zeng jia 。 dan shi , wo men zhi dao you wen ti que bu zhi dao na li you wen ti , suo yi yi ban shi yong Performance Monitor lai yan zheng shi fou you nei cun xie lou , er shi yong BoundsChecker lai zhao dao he jie jue 。    dang Performance Monitor xian shi you nei cun xie lou , er BoundsChecker que wu fa jian ce dao , zhe shi you liang zhong ke neng : di yi zhong , fa sheng le ou fa xing nei cun xie lou 。 zhe shi ni yao que bao shi yong Performance Monitor he shi yong BoundsChecker shi , cheng xu de yun xing huan jing he cao zuo fang fa shi yi zhi de 。 di er zhong , fa sheng le yin shi de nei cun xie lou 。 zhe shi ni yao chong xin shen cha cheng xu de she ji , ran hou zai xi yan jiu Performance Monitor ji lu de Counter de zhi de bian hua tu , fen xi qi zhong de bian hua he cheng xu yun xing luo ji de guan xi , zhao dao yi xie ke neng de yuan yin 。 zhe shi yi ge tong ku de guo cheng , bu man le jia she 、 cai xiang 、 yan zheng 、 shi bai , dan zhe ye shi yi ge ji lei jing yan de jue hao ji hui 。    zong jie    nei cun xie lou shi ge da er fu duo de wen ti , ji shi shi Java he .Net zhe yang you Gabarge Collection ji zhi de huan jing , ye cun zai zhe xie lou de ke neng , bi ru yin shi nei cun xie lou 。 you yu pian fu he neng li de xian zhi , ben wen zhi neng dui zhe ge zhu ti zuo yi ge cu qian de yan jiu 。 qi ta de wen ti , bi ru duo mo kuai xia de xie lou jian ce , ru he zai cheng xu yun xing shi dui nei cun shi yong qing kuang jin xing fen xi deng deng , dou shi ke yi shen ru yan jiu de ti mu 。 jia ru nin you shen me xiang fa , jian yi huo fa xian le mou xie cuo wu , huan ying he wo jiao liu 。
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有