c++visual-c++application-verifier

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


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?


Solution

  • 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.