본문으로 바로가기

메모리 누수 관련 추적하여 LOG로 남기기

category 개발언어/c++ 2016. 6. 13. 12:18

MemoryTracking.zip

메모리 누수 관련 추적하여 LOG로 남기기

 

 

어디서 가져 왔는지 기억이 나지 않는데 참조로 일단 올려 놓자

 

첨부파일 :MemoryTracking.zip

This article describes how to get the callstack for memory leaks in a certain component (EXE or DLL) of your Visual C++ program. The code provided with this article works on WindowsNT. It should work on Windows9x systems as well, although it has not been tested on such systems. There is another restriction, that it only works on Intel x86 machines. It has been written with MS Visual C/C++ 5.0, but it should work fine with Version 6 of the Developer Studio as well. The example program is written with the Active Template Library (ATL), but it does not contain any COM stuff.

Usage

To detect memory leaks in the components of your application, you just have to add the following lines anywhere to the code of your component:

1. 

2.    #include "c:\temp\HookAlloc\MemoryTracking.h"

3.    USE_MEMORYTRACKING("c:\\temp\\allocations.log",true)

4.    #pragma warning(disable:4073)                                              

5.    #pragma init_seg(lib)

  • the path in the #include-statement has to be adapted to your needs
  • The Macro USE_MEMORYTRACKING("c:\\temp\\allocations.log", bLogAtStartup) defines the name of the Logfile (if NULL, all output is written to the debug output window) and specifies, whether logging is on or off at program startup
  • To control the parts of your program where logging is on / off use the macros LOGGING_ON / LOGGING_OFF or BEGIN_NO_LOG_BLOCK / END_NO_LOG_BLOCK to switch off logging for a special part of code within a single function/method.

The pragma you see above switches off the stupid compiler warning that an 'initializer has been put in "lib" initialisation area'. This warning is not needed at all, since we know what we are doing with the next line: MemoryTracking is set up before any of the "users objects" (like MFC's CWinApp derived object) are initialized