본문으로 바로가기

Memory(-Leak) and Exception Trace (CRT and COM Leaks)

category 개발언어/c++ 2016. 7. 16. 17:50

메모리 누수 Check 관련 소스 인데 출처는 잘 기억이 나지 않지만 나중에 참조 할 일이 있어서 일단 보관해 놓는다.

MemLeakAnalyse.zip

Memory_and_Exception_Trace.zip

MFCLeakTest.zip


 

Introduction

With this utility you can simply find memory leaks in your program (CRT and COM-Leaks!). Each leak is displayed with the callstack (including the source line) of the allocation. So, you can easily find leaks, while using the STL. It will also write a file with the callstack if your application crashes (it can also handle stack-overflows!). It almost has no runtime-overhead (runtime-cost). And the best: it is free (GNU Lesser General Public License).

Finding memory leaks

It is easy to implement this in your existing VC code:

  1. All the leaks will be listed in the file YouAppName.exe.mem.log in the application directory (only in debug builds; it is deactivated for release builds). This will also activate exception-handling by default (release and debug builds).

    Only use exception-handling

    If you only want to use exception handing, you need to do the following:

  2. If an exception occurs, it will write a file with the callstack in the application directory with the name YouAppName.exe.exp.log.

    Example

    A simple example is given below:

     Collapse | Copy Code

    #include <span
    								class="code-keyword"><windows.h>
    								

    </span>
    						

    #include <span
    								class="code-string">"Stackwalker.h"
    						

    </span>
    						

     

    void main()
    

    {
    

    
    						// Uncomment the following if you only
    							

    
    							// need the UnhandledException-Filter
    

    
    							// (to log unhandled exceptions)
    

    
    							// then you can remove the "(De)InitAllocCheck" lines
    

    
    							//OnlyInstallUnhandeldExceptionFilter();
    

     

      InitAllocCheck();
    

     

    
    						// This shows how the mem-leak function works
    							

    
    							char *pTest1 = new
    							char[100];
    						

     

    
    						// This shows a COM-Leak
    							

      CoTaskMemAlloc(120);
    						

     

    
    						// This shows the exception handling
    							

    
    							// and log-file writing for an exception:
    

    
    							// If you want to try it, please comment it out...
    

    
    							//char *p = NULL;
    

    
    							//*p = 'A'; // BANG!
    

     

      DeInitAllocCheck();
    

    }
    

    If you execute this example, you will get a file Appication-Name.exe.mem.log with the following content:

     Collapse | Copy Code

    ##### Memory Report ########################################
    

    11/07/02 09:43:56
    

     

    ##### Leaks: ###############################################
    

    RequestID:           42, Removed: 0, Size:          100
    

    1: 11/07/02 09:43:56
    

    1: f:\vs70builds\9466\vc\crtbld\crt\src\dbgheap.c(359)
    

                                  +30 bytes (_heap_alloc_dbg)
    

    1: f:\vs70builds\9466\vc\crtbld\crt\src\dbgheap.c(260)
    

                                  +21 bytes (_nh_malloc_dbg)
    

    1: f:\vs70builds\9466\vc\crtbld\crt\src\dbgheap.c(139) +21 bytes (malloc)
    

    1: f:\vs70builds\9466\vc\crtbld\crt\src\newop.cpp(12) +9 bytes (operator new)
    

    1: d:\privat\memory_and_exception_trace\
    

                memory_and_exception_trace\main.cpp(9) +7 bytes (main)
    

    1: f:\vs70builds\9466\vc\crtbld\crt\src\crt0.c(259)
    

                                   +25 bytes (mainCRTStartup)
    

     

    **** Number of leaks: 1
    

     

    ##### COM-Leaks: ###############################################
    

    (shortened)
    

    **** Number of leaks: 1
    

    Explanation

    Now, I will explain the Memory-Report-File:

     Collapse | Copy Code

    RequestID:           42, Removed: 0, Size:          100
    

    This line is the beginning of one leak. If you have more than one leak, then each leak will start with a RequestID.