0

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

有什么建议么?

4

1 回答 1

4

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

于 2013-11-22T17:06:18.780 回答