2015-01-23 3 views
3

메모리 누수를 찾기 위해 CRT 라이브러리를 사용하고 있습니다. 사실을 발견Visual Studio CRT 메모리 보고서에 CRT 블록이 표시되는 이유

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 바이트 일반 블록을 유출 :

#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; 
} 

나는 다음과 같은 출력을 얻을 :이 같은 일부 코드를 썼다. 나는 그것을 기대했다. 그러나 1 CRT 블록에서이 4096 바이트는 무엇입니까? Microsoft에 따르면 :

CRT 블록은 자체 용도로 CRT 라이브러리에 의해 할당됩니다. CRT 라이브러리는 이러한 블록에 대한 할당 해제를 처리합니다. 따라서 CRT 라이브러리가 손상된 것과 같이 잘못된 것이 아닌 한 메모리 누출 보고서에 이러한 오류가 표시되는 경우는 거의 없습니다.

그냥이 4096 바이트를 무시해야합니까? 감사.

답변

-1

예, CRT 블록은 무시해도됩니다. 할당은 코드에 의해 수행되지 않았으므로 걱정하지 않아도됩니다.

+0

감사합니다. 나는 그것을 upvote하는 데 충분한 명성이 없습니다. – Amber

0

실제로 누수가있는 것 같습니다. 나는 Deleaker와 코드를 확인했고 여기 보이는 것은 내가 뭘 찾았는지 : 또한

leaks

link to the image

를 나는 내가 거기에 충돌하지 않음) 무료로 라인 (에 중단 점을 넣어 경우 두 개의 동일한 값을 입력하는 경우.

두 개의 다른 값을 입력하면 removeDups() 함수에서 free()를 한 번 누르십시오. 한 번만.

분명히 뭔가 잘못되었습니다! 마침내 deleteNodes()를 호출 할 필요가 없습니까?

+0

안녕하세요, Artem, 나를 확인해 주셔서 감사합니다. 예, 누수가 있습니다. 의도적으로 메모리 누수 검사가 작동하는 방식을 확인하기 위해 목록을 비우지 않았습니다. 4096 CRT 블록에 대해 걱정해야하는지 여부를 알지 못했습니다. Anrsimha가 아래에서 말했듯이, Visual Studio에서 관리하므로 무시할 수 있습니다. – Amber

+0

@Amber, 아니요! 당신은 할 수 없습니다. 이것은 실제 누출이며이 기억을 해방시켜야합니다. 물론 단순한 프로젝트에서 큰 문제는 아니지만 실제 프로젝트에서 메모리 누출은 큰 문제입니다. 메모리 부족과 앱 충돌 만 :) –

1

이 4,096 바이트 할당은 처음에 printf을 호출 할 때 할당되는 stdout의 임시 버퍼입니다. 두 번째 메모리 검사 점 사이에 처음으로 printf을 호출하기 때문에 두 검사 점의 차이로 나타납니다. 첫 번째 메모리 검사 점 앞에 printf에 대한 호출을 추가하면이 4,096 바이트 할당이 차이에 나타나지 않습니다.

CRT가 정상적으로 종료되면이 버퍼가 해제됩니다.