반응형
동적할당 시 메모리 delete를 안해주면 메모리가 누수된다.
누수된 부분을 찾는 방법
- int형 400byte 동적할당한다.
int main()
{
int* buff = new int[100];
}
- 전처리 및 함수를 입력한다.
//메모리 누수관련 전처리
#include <iostream>
#define _CRTDBF_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
int main()
{
int* buff = new int[100];
_CrtDumpMemoryLeaks(); //메모리 누수 함수 체크
}
(출력이미지)
출력은 되었지만, 어느 부분에서 메모리 누수 되었는지 알수없다.
- 해결방법
#include <iostream>
//메모리누수 체크 코드
//#include "pch.h"
//#include <Window.h>
#define _CRTDBF_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
//어느 부분인지 체크하는 전처리 및 정의
#ifdef _DEBUG
#define new new (_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
int main()
{
int* buff = new int[100];
_CrtDumpMemoryLeaks();
}
ifdef 부분을 추가하면 다음과 같이 어느부분에서 메모리누수가 발생했는지 알수있다.
또한, 더블클릭하면 그 지점으로 이동한다.
반응형
'C++' 카테고리의 다른 글
[C++] 포인터(Pointer) (0) | 2020.07.23 |
---|---|
[C++] 스택과 힙 (0) | 2020.07.23 |
[C++] char[] 와 char*의 차이 (0) | 2020.07.22 |
[C++] 포인터(Pointer)와 레퍼런스(Reference : 참조자)의 차이 (0) | 2020.07.22 |
[C++] char*, const char*, char* const (0) | 2020.07.21 |