为什么_CrtSetDumpClient不工作? [英] Why is _CrtSetDumpClient not working?

查看:245
本文介绍了为什么_CrtSetDumpClient不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio防爆preSS 2013年Windows桌面用C编写的Windows命令行程序。当在调试模式下进行编译,我真的很喜欢我的程序来检测内存泄漏并打印在标准错误或标准输出,使他们在我的脸上。

I am working on a Windows command-line program written in C using Visual Studio Express 2013 for Windows Desktop. When compiled in Debug mode, I would really like my program to detect memory leaks and print them on the standard error or standard output so they are in my face.

通过调用 _CrtDumpMemoryLeaks ,我能得到内存泄漏信息打印出来,在Visual Studio调试输出(可输出窗格中找到下)。根据MSDN文档上,我认为我可以添加一个调用 _CrtSetDumpClient ,以获得对数据的访问被弃置,然后将其打印到标准错误。

By calling _CrtDumpMemoryLeaks, I am able to get memory leak information printed out to the Debug output in Visual Studio (which you can find under the Output pane). Based on the MSDN documentation, I would think that I can add a call to _CrtSetDumpClient in order to get access to the data being dumped and then print it to stderr.

下面是code我使用来测试此问题:

Here is the code I am using to test this issue:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <stdio.h>
#include <crtdbg.h>

void dumpClient(void * userPortion, size_t blockSize)
{
    printf("memory leak: %d\n", blockSize);
}

int main(int argc, char ** argv)
{
    printf("hello\n");
    _CrtSetDumpClient(&dumpClient);
    malloc(44);
    _CrtDumpMemoryLeaks();
    return 0;
}

我在Visual Studio中取得一个新的Visual C ++的Win32控制台应用程序项目,该卡code到项目中,禁用precompiled头,确信的IDE是在调试模式,并内置。如果我通过pressing F5(开始调试命令)运行它,那么我可以看到在Visual Studio,这是很好的调试窗口中下面的输出:

I made a new Visual C++ Win32 Console Application project in Visual Studio, stuck this code into the project, disabled precompiled headers, made sure that the IDE was in Debug mode, and built. If I run it by pressing F5 (the Start Debugging command), then I can see the following output in the Debug window of Visual Studio, which is good:

Detected memory leaks!
Dumping objects ->
c:\users\david\documents\scraps\test_vc++\testvc\testvc.cpp(15) : {81} normal block at 0x0120A500, 44 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
The program '[3868] TestVC.exe' has exited with code 0 (0x0).

不过,如果我设置在 dumpClient 断点,我可以看到它永远不会被调用。另外,如果我从一个命令提示符下运行该程序,它只是版画你好。在有望的是我希望看到的输出是:

However, if I set a breakpoint inside dumpClient, I can see that it is never being called. Also, if I run the program from a Command Prompt, it just prints hello. The expected output that I would hope to see is:

hello
memory leak: 44

有谁知道为什么 dumpClient 函数没有被调用呢?

推荐答案

TL; DR
你可以叫 _malloc_dbg(44 _CLIENT_BLOCK,文件名,行)而不是的malloc

TL;DR You may call _malloc_dbg(44, _CLIENT_BLOCK, filename, line) instead of malloc.

通过查看dbgheap.c,你可以看到你的函数可以调用的唯一途径是有:

By looking at dbgheap.c, you can see that the only way your function can be called is there:

if (_BLOCK_TYPE(pHead->nBlockUse) == _CLIENT_BLOCK)
{
    _RPT3(_CRT_WARN, "client block at 0x%p, subtype %x, %Iu bytes long.\n",
        (BYTE *)pbData(pHead), _BLOCK_SUBTYPE(pHead->nBlockUse), pHead->nDataSize);

    if (_pfnDumpClient && !__crtIsBadReadPtr(pbData(pHead), pHead->nDataSize))
    {
        (*_pfnDumpClient)((void *)pbData(pHead), pHead->nDataSize);
    }
    else
    {
        _printMemBlockData(plocinfo, pHead);
    }
}

所以,你必须有_BLOCK_TYPE(pHead-> nBlockUse)== _CLIENT_BLOCK。

So you must have _BLOCK_TYPE(pHead->nBlockUse) == _CLIENT_BLOCK.

致电时的malloc ,只分配 _NORMAL_BLOCK 秒。

您可以致电 _malloc_dbg(44 _CLIENT_BLOCK,文件名,行)来代替。
http://msdn.microsoft.com/en-us/library/faz3a37z.aspx

那么你的函数将被调用。

Then your function will be called.

当然,微软可能在 _CrtSetDumpClient 文档中mentionned这一点,但它会一直太容易了;)

Of course Microsoft could have mentionned this in the _CrtSetDumpClient documentation, but it would've been too easy ;)

这篇关于为什么_CrtSetDumpClient不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆