엔진프로그래밍

[매크로 충돌 제거] 디버깅누수 new 매크로와, Assimp new매크로

doyyy_0 2025. 9. 6. 09:04

메모리 leak이 났을 때 출력창에

Detected memory leaks!
Dumping objects ->
C:\Users\Lenovo\source\repos\Dx12Engine\Dx12Engine\Renderer.cpp(398) : {257} normal block at 0x000001CFFEC93A20, 592 bytes long.
 Data: <                > 02 00 00 00 00 00 00 00 CD CD CD CD CD CD CD CD 
Object dump complete.
The program '[10252] Dx12Engine.exe' has exited with code 0 (0x0).

 

이렇게 코드 몇번째 줄 어디서 에러가 떴는지 출력해주기 위해선

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

 

이렇게 new 매크로를 띄워야한다. 그런데, Assimp를 쓰는 도중에 자꾸 에러가 난다. 그 코드를 확인해보니,

struct ASSIMP_API AllocateFromAssimpHeap {
    // http://www.gotw.ca/publications/mill15.htm

    // new/delete overload
    void *operator new(size_t num_bytes)...

 

여기서 에러가 났다.

 

두 new매크로 끼리 충돌을 일으켜서 실행이 되지 않았던 것이다. 어쨌든 둘다 필요하니 assimp헤더파일을 읽을 때,

 

//Assimp에서 new매크로와 메모리누수의 new와 충돌 제거하기 위함(pch.h에 정의됨)
#ifdef new
#undef new  
#endif

#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>


#pragma comment(lib, "assimp-vc143-mtd.lib") 

//메모리 릭 확인 재정의
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

이렇게 new를 없애준다. 그러면 에러가 사라진다.

하지만 기존의 new가 사라졌으니 재정의해준다