请教一个关于内存泄漏的问题。
首先说明一下,我在网上搜到了一些在VC下检测内存泄漏的方法,方法很好用。直接搜索内存泄漏就有。
我的问题是着样的。
比如下面的例子代码
例子很简单就是new了2个数组,一个在函数test中,
函数test将被反复调用。
// test3.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
//#include <process.h>
inline void EnableMemLeakCheck()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
using namespace std;
void test(double* X){
X[0]=1;
double* x=new double[300];
delete [] x;
}
int main()
{
EnableMemLeakCheck();
_CrtMemState s1, s2;
_CrtMemCheckpoint( &s1 );
_CrtMemDumpStatistics( &s1 );
double* xkoll=new double[3];
for (int ii = 0; ii<1000; ii++)
{
test(&xkoll[0]);
}
delete xkoll;
_CrtMemCheckpoint( &s2 );
_CrtMemDumpStatistics( &s2 );
system("pause");
return 0;
}
主函数中的变量xkoll和函数test中的变量X 我都做了delete。
可是我发现当当我多次调用函数test 后,内存报告中的Total allocations: 越来越大。
这属于内存泄漏吗?这是为什么呢?如何解决?
当ii 循环到 1
0 bytes in 0 Free Blocks.
0 bytes in 0 Normal Blocks.
8338 bytes in 43 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 10762 bytes.
Total allocations: 16241 bytes.
当 ii循环到 1000
0 bytes in 0 Free Blocks.
0 bytes in 0 Normal Blocks.
8338 bytes in 43 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 10762 bytes.
Total allocations: 2413841 bytes.