我正在使用运行时库CRT来检测内存泄漏。这是我在析构函数中发现的:
_CrtMemCheckpoint(&crtMemStateFinish);
nDifference = _CrtMemDifference(&crtMemStateDifference, &crtMemStateStart, &crtMemStateFinish);
sizeChange = crtMemStateFinish.lSizes - crtMemStateStart.lSizes;
if(nDifference > 0)
_CrtDumpMemoryLeaks();
nDifference = 0;
sizeChange = 0;
_CrtMemCheckpoint(&crtMemStateStart);
delete[] ptr;
ptr = NULL;
_CrtMemCheckpoint(&crtMemStateFinish);
nDifference = _CrtMemDifference(&crtMemStateDifference, &crtMemStateStart, &crtMemStateFinish);
sizeChange = crtMemStateFinish.lSizes - crtMemStateStart.lSizes;
if(nDifference > 0)
_CrtDumpMemoryLeaks();
nDifference = 0;
sizeChange = 0;
_CrtMemCheckpoint(&crtMemStateStart);
nDifference>0,所以存在内存泄漏,但它没有说明在哪里。
以下是ptr的声明和定义方式:
char ** ptr;
ptr = new char*[4];
有什么建议吗?
您正在分配一个指针数组。您不仅必须删除数组,还必须删除数组中的每个指针。显然,在删除数组之前,您需要执行操作。