[ Programing ]/C++
[Memory Leak Detector] 메모리 누수 체크 _CrtSetDbgFlag(), _CrtDumpMemoryLeaks()
Mister_Q
2010. 2. 4. 11:46
1. 메모리 누스 체크 지원하는 헤더파일 추가한다.
#include <crtdbg.h>
2. 코드 시작 부분에 메모리 누수 검출을 막기 위해 설정한다.
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
- Flag 설정하지 않으면 상속구조에서 메모리 누수가 없어도 누수가 있다고 메시지 나온다.
- STL의 경우에도 메모리 누수에 대한 잘못된 메시지가 나오게 된다.
3. 프로그램 종료시점에 해당하는 부분에 다음과 같이 입력한다.
_CrtDumpMemoryLeaks();
4. 프로그램을 실행하면 메모리 누수가 발생할 경우 누수 발생에 대한 출력 메시지가 디버그 output 창에 나타난다.
-------------------------------------------------------------------
ex )
#include <crtdbg.h>
#include <stdlib.h>
#include <windows.h>
void main()
{
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
..... ( 요약 ) .....
while( WM_QUIT != msg.message )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
if( CheckFrame() )
{
cGameFrame->Update();
cGameFrame->Draw();
}
}
}
_CrtDumpMemoryLeaks();
}
==================================================
=> 실행시 메모리 누수가 체크되면 디버그 output창에서 메모리 누수 결과 메시지를 표시해 준다.
ex )
Detected memory leaks!
Dumping objects ->
{236333} nomal block at 0x02BC31D8, 8 bytes long.
Data: < > 08 00 00 00 00 00 00 00
{236331} nomal block at 0x02B808A0, 8 bytes long.
Data: < > 08 00 00 00 09 00 00 00
..... ( 요약 ) .....
-------------------------------------------------------------------
출처: