Compile errors when I use CRT memory leak detection

To detect a memory leak, the new keyword is redefined. This is normal if I use [Type 1]. But a compilation error occurs if I uncomment [Type 2]. Is there a way to use both types of new ones?

#include <crtdbg.h> #define new new(_CLIENT_BLOCK, __FILE__, __LINE__) struct Foo { int m_N; Foo() : m_N( 0 ) {} }; int main( int argc, char* argv[] ) { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetReportMode(_CRT_WARN , _CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); int* pI = new int( 1 ); delete pI; Foo* pFoo = new Foo; // [Type 1] //Foo* pFoo2 = new (pFoo) Foo(); // [Type 2] return 0; } 
+3
c ++ new-operator
source share
1 answer

Since your macro causes your placement to expand, follow these steps:

 Foo* pFoo2 = new(_CLIENT_BLOCK, __FILE__, __LINE__) (pFoo) Foo(); 

which is clearly invalid.

MSFT engineer confirms :

#define new is not compatible with posting new

therefore you cannot use it with crtdbg.h .

+2
source share

All Articles