删除指针导致内存泄漏?

时间:2013-11-22 17:04:18

标签: c++ memory-leaks microsoft-runtime-library

我正在使用运行时库(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];

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

您正在分配指针数组。您不仅要删除数组,还必须删除数组中的每个指针。显然你需要在之前删除数组