U E D R , A S I H C RSS

Debugging Seminar_2005/DebugCRT

DCRT

Debug C Runtime ν΄λž˜μŠ€λΌ μ΄μš©ν•˜λ©΄ λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ•Œμ•„μ„œ λ™μ ν• λ‹Ήλ˜λŠ” λ©”λͺ¨λ¦¬λΌ μ—­μΆ”μ ν•˜μ—¬ λˆ„μˆ˜λ˜λŠ” λ©”λͺ¨λ¦¬λΌ κ°€λŠ ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
μžκΈ°κ°€ μ§  ν”„λ‘œκ·Έλž¨μœΌλ‘œ 돌렀보고 λˆ„μˆ˜ μ—¬λΆ€λΌ νŒλ‹¨ν•˜κΈ°μ— 쒋을 것이닀.

~cpp _crtDbgFlag

_CRTDBG_ALLOC_MEM_DF 디버그 ν• λ‹Ή ν™œμ„±ν™”. free store μƒνƒœ 좔적
_CRTDBG_DELAY_FREE_MEM_DF λ©”λͺ¨λ¦¬λΌ delete에 μ˜ν•΄ 해지 λ˜μ§€ μ•Šλ„λ‘ν•¨. λ©”λͺ¨λ¦¬ λΆ€μ±μƒν™©ν•˜μ— λ°œμƒν•˜λŠ” 일을 μ•Œ 수 μž‡μŒ
_CRTDBG_CHECK_ALWAYS_DF _CrtCheckMemory() ν•¨μˆ˜λΌ λͺ¨λ“  new, delete ν•¨μˆ˜μ— λŒ€ν•΄μ„œ μžλ™ 호좜 λ˜λ„λ‘ μ§€μ •ν•œλ‹€.
이 ν•¨μˆ˜λŠ” ν• λ‹Ήλœ κ³΅κ°„μ˜ μœ νš¨μ„±μ„ μ§€μ†μ μœΌλ‘œ μ²΄ν¬ν•œλ‹€. 즉 domainerrorλ‚˜ 기타 λ©”λͺ¨λ¦¬ access에 κ΄€ν•œ 뢀뢄을 κ²€μ‚¬ν•œλ‹€. λŒ€μ‹  μ˜€λ²„ν—€λ“œκ°€ μƒλ‹Ήν•˜λ‹€. κ·ΈλŸ¬λ‚˜ 그만큼 λ””λ²„κΉ…μ˜ νš¨μœ¨μ„±μ„ λ†’μ—¬μ„ μˆ˜ μžˆλ‹€.
_CRTDBG_CHECK_CRT_DF λ‚΄λΆ€μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” λ©”λͺ¨λ¦¬κ°€ λ””λ²„κ·Έμž‘μ—…μ„ ν•˜λŠ” λ™μ•ˆ μΆ”μ λœλ‹€.
_CRTDBG_LEAK_CHECK_DF ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ˜λŠ” μ‹œμ μ—μ„œ _CrtDumpMemoryLeaks()λΌ ν˜ΈμΆœ. λ©”λͺ¨λ¦¬ ν•΄μ œμ— μ‹€νŒ¨ν•œ 경우 κ·Έ μ •λ³΄λΌ μ–»μ„ 수 μžˆλ‹€.
_CRTDBG_ALLOC_MEM_DF λŠ” 기본적으둜 on, 기타 ν”Œλž˜κ·ΈλŠ” λ””ν΄νŠΈ off이λ€λ‘œ bitwise μ—°μ‚°μžλΌ μ΄μš©ν•΄μ„œ μ μ ˆν•˜κ²Œ ν”Œλž˜κ·ΈλΌ μ„μ •ν•΄μ•Όν•œλ‹€.

~cpp 
int flas = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;  // ν”Œλž˜κ·Έ on
flag &= !_CRTDBG_LEAK_CHECK_DF;  // ν”Œλž˜κ·Έ off
_CrtSetDbgFlag(flag);

Code

~cpp 
//this define must occur before any headers are included.
//λ°˜λ“œμ‹œ include μ „μ²˜λ¦¬κΈ°μ˜ μ•žλΆ€λΆ„μ— μ„ μ–Έλ˜μ–΄μ•Όν•¨.
#define _CRTDBG_MAP_ALLOC

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

// include crtdbg.h after all other headers.
// μ „μ²˜λ¦¬ λ¬Έμž₯이 λλ‚œλ’€μ— include
#include <crtdbg.h>

int main(int argc, char *argv[]) {
	//turn on the full heap checking
	//DCRT의 μ˜€λ²„ν—€λ“œκ°€ μƒλ‹Ήν•˜κΈ° λ•Œλ¬Έμ— κΈ°λ³Έμ˜΅μ…˜μ΄ κΊΌμ Έμžˆλ‹€κ³  ν•œλ‹€. λ”°λΌμ„œ λ‹€μŒκ³Ό 같이 μ˜΅μ…˜μ„ ν™œμ„±ν™” ν•˜λŠ” μž‘μ—…μ΄ ν•„μš”
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF	|
					_CRTDBG_CHECK_ALWAYS_DF	|
					_CRTDBG_DELAY_FREE_MEM_DF	|
					_CRTDBG_LEAK_CHECK_DF	);

	//Allocate some more memory.
	TCHAR* pNew = new TCHAR[200];
	TCHAR* pNew2 = new TCHAR[200];
	TCHAR* pMemLeak = (TCHAR*)malloc (100);

	_tcscpy( pNew, _T("New'd memory...") );
	_tcscpy( pNew2, _T("more New'd memory...") );
	_tcscpy( pMemLeak, _T("Malloc'd memory...") );

	return EXIT_SUCCESS;
}
MFCμ—μ„œλŠ” 기본적으둜 λ‘œλ“œν•˜μ§€λ§Œ μ½˜μ†”μ—μ„œλŠ” μ‚¬μš©μžκ°€ 직접 지정을 ν•΄μ€˜μ•Ό λ‘œλ“œν•œλ‹€.

CRT 기타 ν•¨μˆ˜

CRT의 κΈ°λ³Έ ν•¨μˆ˜λ“€μ˜ 좜λ ₯은 디버그 λ©”μ‹œμ§€ μœˆλ„μš°μ΄λ‹€. μ΄λΌ λ³€κ²½ν•˜κΈ°μœ„ν•΄μ„œλŠ” _CrtSetReportMode()λΌλŠ” ν•¨μˆ˜λΌ μ΄μš©ν•΄μ„œ 좜λ ₯μ—λŒ€ν•œ 일반 λͺ©μ μ§€λΌ μ§€μ •ν•˜κ³ , _CrtSetReportFile()λΌ μ΄μš©ν•΄μ„œ νŠΉλ³„ν•œ 슀트림 λͺ©μ μ§€λΌ μ„μ •ν•΄μ•Όν•œλ‹€.
~cpp int _CrtSetReportMode(int reportType, int reportMode);

reportType
_CRT_WARN κ²½κ³  λ©”μ‹œμ§€ 예)memory leak
_CRT_ERROR λ³΅κ΅¬λΆˆκ°€ν•œ 치λͺ…적 μ—λŸ¬
_CRT_ASSERT assertion 좜λ ₯(assert() 좜λ ₯이 μ•„λ‹ˆλ‹€)
crtdbg.hλŠ” ASSERT, ASSERTEλΌλŠ” λ©”ν¬λ‘œ ν•¨μˆ˜λΌ μƒμ„±ν•œλ‹€. λ‘˜μ˜ 차이점은 ASSERTEλŠ” assertion ν‘œν˜„μ„ λ³΄κ³ ν•˜κ³ , λ‹€λ₯Έ 것은 ν•˜μ§€ μ•ŠλŠ” λ‹€λŠ” 것이닀.

reportMode
_CRTDBG_MODE_DEBUG 디버그 μœˆλ„μš° μ•ˆμ—μ„œ 보게될 디버그 λ¬Έμžμ—΄μ— 좜λ ₯을 전달
_CRTDBG_MODE_FILE output stream
_CRTDBG_MODE_WNDW λ©”μ‹œμ§€ λ°•μŠ€
_CRTDBG_REPORT_MODE ν˜„μž¬μ˜ 리포트 λͺ¨λ“œλΌ λ¦¬ν„΄ν•œλ‹€.
닀쀑 λͺ©μ μ§€ 지정은 flag μ„μ •κ³Ό λ§ˆμ°¬κ°€μ§€λ‘œ | μ—°μ‚°μžλΌ ν†΅ν•΄μ„œ κ°€λŠ₯ν•˜λ‹€.


~cpp _HFILE _CrtSetReportFile(int reportType, _HFILE reportFile);
λ‘λ²ˆμ§Έ μΈμˆ˜λŠ” 파일 슀트림의 _HFILE ν˜•μ‹μ˜ ν¬μΈν„°μ΄κ±°λ‚˜ λ‹€μŒμ˜ μ‹λ³„μžλ“€ μ€‘μ˜ ν•˜λ‚˜μ΄λ‹€.
_CRTDBG_FILE_STDERR ν‘œμ€ μ—λŸ¬ 슀트림으둜 전달
_CRTDBG_FILE_STDOUT ν‘œμ€ μΆœλ ₯ 슀트림으둜 전달
_CRTDBG_REPORT_FILE ν˜„μž¬μ˜ λͺ©μ μ§€λΌ λ¦¬ν„΄ν•œλ‹€.

output in debug console (vc++6)

dcrt_output_debug_console.jpg

after

λŒ€μ²΄λ‘œ ν΄λž˜μŠ€μ—μ„œμ˜ λ©”λͺ¨λ¦¬ μœ μΆœμ€ 동적 멀버 λ³€μˆ˜λ“€μ΄ λ§Œλ“€μ–΄μ‘Œμ„ λ•Œ ν•΄μ•Όν•˜λŠ” 'μ‚Όμ˜ 법칙'을 지킀지 μ•ŠλŠ”λ°μ„œ λΉ„λλ˜λŠ” κ²½μš°κ°€ λ§Žλ‹€.
동적 λ©€λ²„λ³€μˆ˜λΌ μ‚¬μš©ν•˜λŠ” κ²½μš°μ—λŠ” λŒ€μž…, ν• λ‹Ή μƒμ„±μž, μ†Œλ©ΈμžλΌ λ°˜λ“œμ‹œ λ§Œλ“€μ–΄μ•Όν•œλ‹€.
μ°Έμ‘°) The rule of Three

related

after

DCRTλΌ μΌœκ³  λ„λŠ” 것에 λ”°λΌμ„œ ν”„λ‘œκ·Έλž¨μ˜ μˆ˜ν–‰μ†λ„μ˜ 차이가 μƒλ‹Ήν•˜λ‹€. STLλ•Œλ¬ΈμΈμ§€λ„ λͺ¨λ₯΄κ² μ§€λ§Œ. - eternalbleu

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:06
Processing time 0.0240 sec