๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ๊ด€๋ จ๋œ ํฌ์ŠคํŒ…์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

[๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์†Œ๊ฐœ

 

[๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์†Œ๊ฐœ

์†Œ๊ฐœ Unmanaged Code์ธ C++์€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ํ•ด์ œ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ  ํ•ด์ œ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ทธ๋Œ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ ์œ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋Š” ์–ด๋–ป๊ฒŒ ์ ๊ฒ€ํ• ๊นŒ์š”? ์ ..

luckygg.tistory.com

์ง€๋‚œ ํฌ์ŠคํŒ…์„ ํ†ตํ•ด CRTDBG๋ฅผ ์ด์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์ ๊ฒ€ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

[๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• - CRTDBG (์˜ˆ์ œ ํฌํ•จ)

 

[๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• - CRTDBG (์˜ˆ์ œ ํฌํ•จ)

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ๊ด€๋ จ๋œ ํฌ์ŠคํŒ…์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. [๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์†Œ๊ฐœ [๋””๋ฒ„๊น…] ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ์ ๊ฒ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์†Œ๊ฐœ ์†Œ๊ฐœ Unmanaged Code์ธ C++์€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ํ•ด์ œ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€..

luckygg.tistory.com

๊ทธ๋Ÿฐ๋ฐ CRT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ๋‹ค๋ฅธ ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ๋” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์„ ํ†ตํ•ด CRT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์Šค๋ƒ…์ˆ์„ ์ด์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์ ๊ฒ€ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

CRT ์Šค๋ƒ…์ˆ

CRT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” _CrtMemState๋ผ๋Š” ๊ตฌ์กฐ์ฒด๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ตฌ์กฐ์ฒด๋Š” ๋””๋ฒ„๊ทธ ํž™(Debug Heap)์˜ ์ฒซ ๋ฒˆ์งธ block ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•œ _CrtMemCheckpoint ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ˜ธ์ถœ๋œ ์ˆœ๊ฐ„์— ๋””๋ฒ„๊ทธ ํž™์˜ ํ˜„์žฌ ์ƒํƒœ์— ๋Œ€ํ•œ ์Šค๋ƒ…์ˆ์„ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋Š” _CrtMemState์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ ์Šค๋ƒ…์ˆ์€ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋น„๋กฏํ•œ ์—ฌ๋Ÿฌ ์ •๋ณด๊ฐ€ ์žˆ์œผ๋ฉฐ _CrtMemDifference๋กœ ๋‘ ์Šค๋ƒ…์ˆ์˜ ์ฐจ์ด์ ์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ๋ฅผ ์ ๊ฒ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ด๋Ÿฐ ์ ๊ฒ€์€ ๋””๋ฒ„๊ทธ ๋ชจ๋“œ ์ผ ๋•Œ์—๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

C++ Console ์˜ˆ์‹œ

์ง์ ‘ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

#include "pch.h"
#include <Windows.h>
#include <iostream>

#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>

#ifdef _DEBUG
#define new new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#endif

int main() 
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	_CrtMemState s1;
	_CrtMemCheckpoint(&s1);
	_CrtMemDumpStatistics(&s1);

	byte* ptr = new byte[100];

	//delete[] ptr;
    
	_CrtMemState s2;
	_CrtMemCheckpoint(&s2);
	_CrtMemDumpStatistics(&s2);

	_CrtMemState s3;
	if (_CrtMemDifference(&s3, &s1, &s2) == true)
		_CrtMemDumpStatistics(&s3);
}

_CrtMemState๋Š” ์Šค๋ƒ…์ˆ ์ •๋ณด๋ฅผ ๊ฐ–๋Š” ๊ตฌ์กฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์ „์ธ s1, ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ํ›„์ธ s2 ๊ทธ๋ฆฌ๊ณ  s1๊ณผ s2์˜ ์ฐจ์ด์  ์ •๋ณด๋ฅผ ๊ฐ–๋Š” s3์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

_CrtMemCheckpoint๋Š” ์Šค๋ƒ…์ˆ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์ „, ํ›„์— ์Šค๋ƒ…์ˆ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

_CrtMemDifference๋Š” ๋‘ ์Šค๋ƒ…์ˆ์˜ ์ •๋ณด๋ฅผ ๋น„๊ตํ•˜๊ณ , ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฉด ์ฐจ์ด์  ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

_CrtMemDumpStatistics๋Š” ์Šค๋ƒ…์ˆ ์ •๋ณด ๊ตฌ์กฐ์ฒด์ธ _CrtMemState๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ์ถœ๋ ฅ ์ฐฝ์— ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

๋””๋ฒ„๊ทธ ๋ชจ๋“œ๋กœ ์‹คํ–‰ ํ›„ ์ข…๋ฃŒํ•˜๋ฉด, ์•„๋ž˜์˜ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

// ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์ „ ๋ฉ”์‹œ์ง€
0 bytes in 0 Free Blocks.
0 bytes in 0 Normal Blocks.
19122 bytes in 113 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 19122 bytes.
Total allocations: 44955 bytes.

// ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ํ›„ ๋ฉ”์‹œ์ง€
0 bytes in 0 Free Blocks.
100 bytes in 1 Normal Blocks.
19122 bytes in 113 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 19222 bytes.
Total allocations: 45055 bytes.

// ๋‘ ์Šค๋ƒ…์ˆ์˜ ์ฐจ์ด ๋ฉ”์‹œ์ง€
0 bytes in 0 Free Blocks.
100 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 100 bytes.
Total allocations: 100 bytes.

Detected memory leaks!
Dumping objects ->
c:\users\gg\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp(22) : {164} normal block at 0x000A6010, 100 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.

์ฐจ์ด์  ์ •๋ณด๋ฅผ ๋ณด๋ฉด Total allocations: 100 bytes.๊ฐ€ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ delete[] ptr;์„ ์ •์ƒ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–จ๊นŒ์š”?

0 bytes in 0 Free Blocks.
0 bytes in 0 Normal Blocks.
15026 bytes in 112 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 16530 bytes.
Total allocations: 40859 bytes.

// ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ํ•ด์ œ ํ›„
0 bytes in 0 Free Blocks.
0 bytes in 0 Normal Blocks.
15026 bytes in 112 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 16530 bytes.
Total allocations: 40959 bytes.

์œ„ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, ๋‘ ์Šค๋ƒ…์ˆ์˜ ์ •๋ณด๊ฐ€ ๋™์ผํ•˜์—ฌ ๋”ฐ๋กœ ์ถœ๋ ฅ๋œ ๋ฉ”์‹œ์ง€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฉ”์‹œ์ง€๋„ ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Exam_MemoryLeak2.zip
0.00MB

Buy Me A Coffee

๋ฐ˜์‘ํ˜•