2

我正在尝试学习如何使用 umdh.exe 解决内存泄漏问题。为了做到这一点,我编写了一个示例应用程序:

void MemoryLeakTest(int argc, _TCHAR* argv[]);

int _tmain(int argc, _TCHAR* argv[])
{
    MemoryLeakTest(argc, argv);
    return 0;
}

void MemoryLeakTest(int argc, _TCHAR* argv[])
{
    if (argc != 3)
    {
        wcout << "HelloWorld.exe <number of allocations> <number of bytes>" << endl;
        getchar();
        return;
    }

    int numAllocations = _wtoi(argv[1]);
    int numBytes = _wtoi(argv[2]);

    wcout << "Num allocations: " << numAllocations << ", num bytes: " << numBytes << endl;
    wcout << "Press any key to start..." << endl; 
    getchar();

    for (int i = 0; i < numAllocations; ++i)
    {
        void* unusedMemory = new byte[numBytes];

        if ((i % (numAllocations / 100)) == 0)
        {
            wcout << i << endl;
            Sleep(100);
        }
    }

    wcout << "Press any key to finish..." << endl;
    getchar();
}

编译(发布),配置好umdh.exe:

  1. 设置 _NT_SYMBOL_PATH= http://msdl.microsoft.com/symbols/download;C :\TestProjects\HelloWorld\x64\Release;
  2. gflags.exe /i HelloWorld.exe +ust

然后做了实验:

  1. HelloWorld.exe 10000 1000
  2. 等到它停止@first getchar
  3. umdh.exe -pn:helloworld.exe -f:c:\first.log
  4. 继续执行,等到它停止@第二个 getchar
  5. umdh.exe -pn:helloworld.exe -f:c:\second.log

我注意到无论我调用哪个参数,无论是 100 个分配还是 1000 个分配,first.log 和 second.log 看起来都非常相似(针对不同的实验)。

  1. umdh.exe -dc:\first.log c:\second.log -f:c:\result.log

然后我得到唯一只有17 个分配的堆栈(而不是预期的1000):

+   17000 (  17000 -      0)     17 allocs  BackTraceB5023D4D
+      17 (     17 -      0)    BackTraceB5023D4D   allocations

ntdll!memset+165B9
MSVCR120!malloc+5B
MSVCR120!operator new+1F
HelloWorld!MemoryLeakTest+108 (c:\my\main\private\testproject\debugit\helloworld\helloworld.cpp, 40)
HelloWorld!wmain+9 (c:\my\main\private\testproject\debugit\helloworld\helloworld.cpp, 16)
HelloWorld!__tmainCRTStartup+10F (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c, 623)
KERNEL32!BaseThreadInitThunk+D
ntdll!RtlUserThreadStart+1D

更新:TaskProcess 显示私有字节和提交大小都在预期值上增长。

UPDATED2:DebugDiag 正确显示了 10000 个分配和一些指向正确位置的采样调用堆栈的泄漏。

知道我做错了什么吗?

谢谢!

4

0 回答 0