Why is _CrtSetDumpClient not working?

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

By calling _ CrtDumpMemoryLeaks , I can get the memory leak information printed on the Debug output in Visual Studio (which you can find in the Output Panel section). Based on the MSDN documentation, I think I can add a call to _ CrtSetDumpClient to access the dumped data, and then print to stderr.

Here is the code I use to test this problem:

#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 ++ Win32 Console Visual Studio, , , , IDE . , F5 ( " " ), Debug Visual Studio, :

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, , . , , hello. , :

hello
memory leak: 44

- , dumpClient ?

+1
2

TL; DR _malloc_dbg(44, _CLIENT_BLOCK, filename, line) malloc.

dbgheap.c, , , :

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.

malloc _NORMAL_BLOCK s.

_malloc_dbg(44, _CLIENT_BLOCK, filename, line). http://msdn.microsoft.com/en-us/library/faz3a37z.aspx

.

, Microsoft _CrtSetDumpClient, ;)

+1

MSDN

_CrtSetDumpClient: Installs an application-defined function to 
 dump _CLIENT_BLOCK type memory blocks 

_CLIENT_BLOCK. . malloc _NORMAL_BLOCK , , .

0

All Articles