views:

169

answers:

1

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  A: 

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.

JaredPar
It does it by many different methods though, but I just wanted to simplify to find a generic solution.
Brian R. Bondy