ApplicationVerifier is not detecting handle leaks, what do I do?

≯℡__Kan透↙ 提交于 2020-01-17 13:44:13

问题


I did select the executable correctly, because I can get it to respond to certain things I do. But I can't get ApplicationVerifier to properly detect a handle leak.

Here is an example:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    HANDLE hFile = CreateFile(_T("C:\\test.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    return 0;
}

ApplicationVerifier doesn't detect this.

What can I do to detect the above problem?


回答1:


Is your code only creating handles through CreateFile? If so you can just macro these methods out to versions that do custom implemented leak detection. It's a lot of work but it will get the job done.

#if DEBUG
#define CreateFile DebugCreateFile
#define CloseHandle DebugCloseHandle
#endif
// in another cpp file
#undef CreateFile
#undef CloseHandle
HANDLE DebugCreateFile(...) {
  HANDLE real = ::CreateFile(...);
  TrackHandle(real);
  return real;
}
void DebugCloseHandle(HANDLE target) {
  if (IsTracked(target)) { Untrack(target); }
  ::CloseHandle(target);
}
void CheckForLeaks() {
  // Look for still registered handles
}

At the end of your program you'd need to call CheckForLeaks. Like I said though, quite a bit of work but it may help with your scenairo.



来源:https://stackoverflow.com/questions/318420/applicationverifier-is-not-detecting-handle-leaks-what-do-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!