我正在练习使用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字节正常数据块。我早就料到了。但是,在1CRT块中的4096字节是什么?根据微软的说法:
CRT块由CRT库分配给自己使用。CRT库处理这些块的释放。因此,您不太可能在内存泄漏报告中看到这些,除非出现重大错误,例如,CRT库已损坏。
我应该忽略这4096个字节吗?谢谢。
发布于 2015-02-08 14:27:05
这个4,096字节的分配是第一次调用printf时分配的stdout的临时缓冲区。因为第一次调用printf是在两个内存检查点之间,所以它显示为两个检查点之间的差异。如果在第一个内存检查点之前添加对printf的调用,则此4,096字节分配不会出现在差异中。
当CRT正常终止时,该缓冲区被释放。
发布于 2015-01-23 18:07:50
看起来确实有一些漏洞。我已经用Deleaker检查了你的代码,看看我得到了什么:

另外,如果我在free()的行上放了一个断点,在输入两个相同的值的情况下,我永远不会命中那里。
如果我输入两个不同的值,我只会点击一次free() (在removeDups()函数中)。只有一次。
显然,代码出了问题!最后不需要调用deleteNodes()吗?
发布于 2015-01-23 13:22:05
是的,可以安全地忽略CRT块。分配不是由您的代码完成的,因此您不必为此操心
https://stackoverflow.com/questions/28102835
复制相似问题