首先说明一下,我在网上搜到了一些在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.

解决方案 »

  1.   

    delete xkoll;
    --------------------
    delete []xkoll;
      

  2.   

    哦 谢谢,不过问题照旧,新的debug信息如下。'test3.exe': Loaded 'D:\Eigene Dateien\Visual Studio 2008\test3\Debug\test3.exe', Symbols loaded.
    'test3.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
    'test3.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
    'test3.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
    'test3.exe': Loaded 'C:\WINDOWS\system32\shimeng.dll'
    'test3.exe': Unloaded 'C:\WINDOWS\system32\shimeng.dll'
    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: 8974 bytes.
    Total allocations: 13817 bytes.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.'test3.exe': Loaded 'C:\WINDOWS\system32\apphelp.dll'
    'test3.exe': Loaded 'C:\WINDOWS\system32\version.dll'
    'test3.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'
    'test3.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'
    'test3.exe': Loaded 'C:\WINDOWS\system32\secur32.dll'
    The program '[3216] test3.exe: Native' has exited with code 0 (0x0).
      

  3.   

    问题表现为,当我在windows下用三指阐,查看内存使用情况时,发现在循环内内存使用持续增高。
      

  4.   

    释放的内存可能并不一定马上回收,还会占用,只有再使用的时候才会使用delete释放的。
      

  5.   

    你的意思是说当程序跑到无内存可用的时候才释放那些delete的地方吗?
    事实上我的程序本身就需要大快内存,如果是着样的话是不是不太好呢?怎样解决呢?
    还是说着种结构就不好?(循环内反复new delete)
      

  6.   

    释放的内存可能并不一定马上回收,还会占用,只有再使用的时候才会使用delete释放
      

  7.   

    主函数中的变量xkoll和函数test中的变量X 我都做了delete。
    可是我发现当当我多次调用函数test 后,内存报告中的Total allocations: 越来越大。
    这属于内存泄漏吗?这是为什么呢?如何解决?
    ====================================
    这个不属于内存泄漏,这个工具的Total allocations只是告诉你,你运行的程序总共分配了这么多内存。你的循环体中不断的new/delete,当然也属于你程序分配的内存了。