C++内存泄漏的检查定位
大家好,近日编程遇到些问题,涉及内存管理。问题如下:
需求是建立一个树形结构,用于快速查找。在构造函数中进行了内存分配
Tree::Tree()
{
_leaf = (TreeData*)malloc(sizeof(TreeData));
_leaf->element = NULL;
_leaf->firstChild = NULL;
_leaf->parent = NULL;
_leaf->rightNeighbor = NULL;
_leaf->rightBroTree = NULL;
}
在建树过程中同样有类似的操作。
为了定位内存泄漏的地方使用如下方法:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG //重载new
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
_CrtMemCheckpoint(&s1);
//Tree tree; //此处为函数实现...
_CrtMemCheckpoint(&s2);
if (_CrtMemDifference(&s3, &s1, &s2))
_CrtMemDumpStatistics(&s3);
_CrtDumpMemoryLeaks();
_CrtMemDumpStatistics这个函数的输出结果为:
0 bytes in 0 Free Blocks.
20 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 20 bytes.
问题1:从上面的输出结果中如何看出内存泄漏(好像有输出就有泄漏),如何定位到泄漏的地方
_CrtDumpMemoryLeaks();函数的输出结果为:
{10018} normal block at 0x04971BF8, 52 bytes long.
Data: <x P > 78 F3 95 04 C8 F4 95 04 50 0F 96 04 10 11 96 04
{10017} normal block at 0x0497AB28, 112 bytes long.
Data: < 9 h: > 18 39 93 04 68 3A 93 04 08 11 94 04 C8 12 94 04
..\src\mesh.cpp(67) : {10016} normal block at 0x0497AAC8, 32 bytes long.
Data: < > 08 A9 97 04 01 CD CD CD 00 00 00 00 00 00 00 00
..\src\mesh.cpp(63) : {10015} normal block at 0x0497AA58, 48 bytes long.
问题2:从这个结果中可以看出来在mesh.cpp中63,67行有内存泄漏,但是上面几个normal block的信息如何定位[i][/i]
编程半路出家,很粗糙,先谢过了!