我确实选择了正确的可执行文件,因为我可以让它回应我做的某些事情。但我无法让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没有检测到这一点。
如何检测上述问题?
答案 0 :(得分:1)
您的代码是否仅通过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。就像我说的那样,相当多的工作,但它可能有助于你的scenairo。