为什么Visual Studio CRT内存报告显示CRT块

时间:2015-01-23 04:03:10

标签: c++ visual-studio memory-leaks crt

我正在练习使用CRT库来查找内存泄漏。我写了一些像这样的代码:

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

typedef struct NodeLL {
    int value;
    struct NodeLL *next;
} Node;

void printLL(Node *pHead) {
    int i=0;
    while(pHead) {
        printf("%d\n", pHead->value);
        i++;
        pHead = pHead->next;
    }
}

Node * addNode(Node *pHead, int value) {
    Node *pNew, *pLL;
    pNew = (Node *)malloc(sizeof(Node));
    pNew->value = value;
    pNew->next = NULL;
    if(!pHead) {
        pHead = pNew;
    }
    else {
        pLL = pHead;
        while(pLL->next) 
            pLL = pLL->next;
        pLL->next = pNew;
    }

    return pHead;
}

void deleteNodes(Node *pHead) {
    Node *pLL;
    int i=0;
    while(pHead) {
        printf("deleting node %d, value is %d\n", i, pHead->value); 
        i++;
        pLL = pHead->next;
        free(pHead);
        pHead = pLL;
    }
}


Node * removeDups(Node *pHead) {
    if (!pHead)
        return NULL;
    Node *pNode2, *pPrev;
    Node *pNode = pHead;
    while(pNode) {
        pPrev = pNode;
        pNode2 = pNode->next; 
        while(pNode2) {
            if(pNode2->value == pNode->value) {
                pPrev->next = pNode2->next;
                free(pNode2);
                pNode2 = pPrev->next;
            }
            else {
                pPrev = pNode2;
                pNode2 = pNode2->next;
            }
        }
        pNode = pNode->next;
    }
    return pHead;
}

int main() {

    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
    //_CrtSetBreakAlloc(71);   // used to break at the second malloc

    _CrtMemState s1, s2, s3;

    // take a snap shot of memory before allocating memory
    _CrtMemCheckpoint(&s1);

    int NodeNum, i, j, value;
    Node *pHead = NULL;

    printf("How many nodes in the linked list?");
    scanf("%d", &NodeNum); 
    for (i=0; i<NodeNum; i++) {
        printf("Please enter Node %d value:", i);
        scanf("%d", &value);
        pHead = addNode(pHead, value);
    }


    printLL(pHead);
    printf("remove duplicates\n");
    pHead = removeDups(pHead);
    printLL(pHead);
    // clean up
    //deleteNodes(pHead);

    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
    _CrtDumpMemoryLeaks();


    // take a snap shot of memory after allocating memory
    _CrtMemCheckpoint(&s2);

    if(_CrtMemDifference(&s3, &s1, &s2) ) 
        _CrtMemDumpStatistics(&s3);

    return 0;
}

我得到以下输出:

Detected memory leaks!

Dumping objects -> ...

\2_1_removedupll\removedupsll.cpp(23) : {72} normal block at 0x00701570, 8 bytes long.
 Data: <        > 03 00 00 00 00 00 00 00 

\2_1_removedupll\removedupsll.cpp(23) : {71} normal block at 0x00701528, 8 bytes long.
 Data: <    p p > 02 00 00 00 70 15 70 00 

\2_1_removedupll\removedupsll.cpp(23) : {70} normal block at 0x007014E0, 8 bytes long.
 Data: <    ( p > 01 00 00 00 28 15 70 00 

Object dump complete.

0 bytes in 0 Free Blocks.

24 bytes in 3 Normal Blocks.

*4096 bytes in 1 CRT Blocks.*

0 bytes in 0 Ignore Blocks.

0 bytes in 0 Client Blocks.

Largest number used: 3870 bytes.

Total allocations: 4120 bytes.

它发现泄漏了24个字节的普通块。我期望。但是1个CRT块中的这4096个字节是多少?根据微软的说法:

CRT库由CRT库分配供自己使用。 CRT库处理这些块的释放。因此,除非出现严重错误,否则您不太可能在内存泄漏报告中看到这些内容,例如,CRT库已损坏。

我应该忽略这个4096字节吗?感谢。

3 个答案:

答案 0 :(得分:1)

这个4,096字节的分配是stdout的临时缓冲区,它是在您第一次调用printf时分配的。因为您第一次呼叫printf是在两个内存检查点之间,所以它看起来是两个检查点之间的差异。如果在第一个内存检查点之前添加对printf的调用,则这个4,096字节的分配不会出现在差异中。

当CRT正常终止时,将释放此缓冲区。

答案 1 :(得分:0)

似乎确实存在一些泄漏。我已经使用Deleaker检查了您的代码并查看了我的内容:

leaks

link to the image

此外,如果我在free()行放置断点,在输入两个相同值的情况下,我从未点击过。

如果我输入两个不同的值,我点击free()一次(在removeDups()函数中)。只有一次。

显然代码出了问题!你最近不需要调用deleteNodes()吗?

答案 2 :(得分:-1)

是的,可以安全地忽略CRT块。分配不是由你的代码完成的,所以你不必为此烦恼