VC本身就可以检查程序里堆中内存的泄露,对于GDI资源那就要自己小心了。

解决方案 »

  1.   

    如果你用MFC,看看CMemoryState。。The following instructions and example show you how to detect a memory leak.To detect a memory leak Create a CMemoryState object and call the Checkpoint member function to get the initial snapshot of memory.
    After you perform the memory allocation and deallocation operations, create another CMemoryState object and call Checkpoint for that object to get a current snapshot of memory usage.
    Create a third CMemoryState object, call the Difference member function, and supply the previous two CMemoryState objects as arguments. The return value for the Difference function will be nonzero if there is any difference between the two specified memory states, indicating that some memory blocks have not been deallocated. 
    The following example shows how to check for memory leaks:// Declare the variables needed
    #ifdef _DEBUG
        CMemoryState oldMemState, newMemState, diffMemState;
        oldMemState.Checkpoint();
    #endif    // do your memory allocations and deallocations...
        CString s = "This is a frame variable";
        // the next object is a heap object
        CPerson* p = new CPerson( "Smith", "Alan", "581-0215" );#ifdef _DEBUG
        newMemState.Checkpoint();
        if( diffMemState.Difference( oldMemState, newMemState ) )
        {
            TRACE( "Memory leaked!\n" );
        }
    #endifNotice that the memory-checking statements are bracketed by #ifdef _DEBUG / #endif blocks so that they are compiled only in Win32 Debug versions of your program.
      

  2.   

    VC自己可以检查一些内存泄露,不过,你也可以在程序开始处用CMemoryState来读取内存,在程序退出时再读取,比较两者是否一致,如果不一致,则有内存泄露,也可以通过工具BoundChecker来检查。
      

  3.   

    to zzh:
        how to find BoundChecker?Thanks!