- 메모리 누수 감지 기능.
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, // 프로세스 시작 주소. 가상 메모리 사용.
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// memory leak detection
// _CRTDBG_ALLOC_MEM_DF: 할당된 메모리 블록을 추적.
// _CRTDBG_LEAK_CHECK_DF: 프로그램 종료 시 할당된 메모리 블록 중 해제되지 않은 블록을 보고.
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
new int;
main.cpp 에서 프로그램의 시작 부분에 넣고 디버그 모드로 돌려보면

비주얼 스튜디오 출력 윈도우에 다음과 같이 뜸. 출력 로그를 보면 233 이라는 숫자를 볼 수 있다.
이게 233 번 할당 번호에서 메모리 누수가 발생했다는 뜻이고 이를
_CrtSetBreakAlloc(0); // 특정 할당 번호에서 중단점을 설정. 0이면 중단점 없음.
이 함수에 넣고 다시 실행해보면, 중단점이 걸리면서 함수 콜스택을 통해
메모리 누수를 일으키는 코드 부분을 찾을 수 있는 듯.
다만 메모리 할당 번호는 랜덤하게 바뀔 수 있기 때문에, 비주얼 스튜디오를 껐다 키거나 하면 이 번호는 달라질 수 있다.
그래서 쓰고 나면 주석 처리해주도록 하자.
- Resource Manager 구현.
이전에 Player 에서 텍스처를 로드했는데, 그러면 객체마다 새로운 텍스처를 만들고 로드하기 때문에 메모리 낭비임.
그래서 리소스를 한곳에 모아 관리하는 매니저가 필요함.
// CResourceMgr.h
#pragma once
class CTexture;
class CResourceMgr
{
SINGLETON(CResourceMgr);
private:
map<wstring, CTexture*> m_mapResources;
public:
void init();
public:
CTexture* LoadTexture(const wstring& _strKey, const wstring& _strRelativePath);
};
강의에서는 Resource Manager 라고 해놓고 Texture 만 로드하는데, 아마 추후에 수정할 듯?
매니저 클래스이므로 싱글톤 처리해주고, 이름-리소스 쌍을 원소로 하는 map 에 리소스를 저장함.
// CResourceMgr.cpp
...
CTexture* CResourceMgr::LoadTexture(const wstring& _strKey, const wstring& _strRelativePath)
{
map<wstring, CTexture*>::iterator iter = m_mapResources.find(_strKey);
if (iter != m_mapResources.end())
{
return iter->second;
}
wstring strFilePath = CPathMgr::GetInstance()->GetContentPath();
strFilePath += _strRelativePath;
CTexture* pTexture = new CTexture();
pTexture->SetKey(_strKey);
pTexture->SetRelativePath(_strRelativePath);
pTexture->Load(strFilePath);
m_mapResources.insert(make_pair(_strKey, pTexture));
return pTexture;
}
이름과 상대경로 값을 인자로 받아서 텍스처를 로드하는 함수.
이전에 Player 에서 로드하던 코드를 복붙하면 됨.
- CTexture 클래스 생성자 private 처리.
// CTexture.h
...
private:
CTexture();
virtual ~CTexture();
friend class CResourceMgr;
리소스는 함부로 생성되지 못하게 오직 ResourceManager 에서만 생성하도록 함.
'Win32 api' 카테고리의 다른 글
| Win32 api 강의 27 - 28화. (0) | 2025.09.18 |
|---|---|
| Win32 api 강의 25 - 26화. (0) | 2025.09.17 |
| Win32 api 강의 23화. (0) | 2025.09.16 |
| Win32 api 강의 21 - 22화. (0) | 2025.09.16 |
| Win32 api 강의 17 - 18화. (0) | 2025.09.15 |