c++ - ApplicationVerifier 未检测到句柄泄漏,我该怎么办?

标签 c++ visual-c++ application-verifier

我确实选择了正确的可执行文件,因为我可以让它响应我所做的某些事情。但是我无法让 ApplicationVerifier 正确检测句柄泄漏。

这是一个例子:

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 没有检测到这一点。

如何检测上述问题?

最佳答案

您的代码是否仅通过 CreateFile 创建句柄?如果是这样,您可以将这些方法宏设置为执行自定义实现的泄漏检测的版本。这是很多工作,但它会完成工作。

#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
}

在程序结束时,您需要调用 CheckForLeaks。就像我说的,虽然需要做很多工作,但它可能对您的场景有所帮助。

关于c++ - ApplicationVerifier 未检测到句柄泄漏,我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/318420/

相关文章:

visual-c++ - 如何在 64 位 Windows 7 上注册 64 位 COM dll?

c++ - 如何在 Visual Studio 2013 中使用带/不带 Microsoft Application Verifier 的调试

c++ - 我可以传递伪装成数组的常量指针吗?

c++ - 在该命名空间中的另一个函数内向前声明一个命名空间中的函数

c++ - 在 Linux 中发送空的 Socket 缓冲区?

visual-c++ - vector<int> - 缺少类型说明符

c++ - App Verifier 错误模式 `fafa` 的意义是什么?

c++ - 为什么要有头文件和 .cpp 文件?

c++ - 如何将 istream * 转换为字符串或仅打印它?