title: Override new operator in C++ while crtdbg.h is causing conflicts
body: <p>While trying out some memory tracking and preparation for my own memory manager, I tried to override the new operator. The article on flipcode was my main guideline in this process ( <a href="http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml" rel="nofollow">http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml</a> ).</p>
  
  <p>After implementing the techniques described in that article, I am left with the problem that somewhere in the STL the "crtdbg.h" is being included either directly or indirectly through some of the header-files that are being included (Using Visual Studio 2010).</p>
  
  <p>This results in an error:</p>
  
  <pre><code>[...]10.0\vc\include\crtdbg.h(1078): error C2365: 'operator new' : redefinition; previous definition was 'function'
  [...]10.0\vc\include\crtdbg.h(1078): error C2078: too many initializers
  [...]
  </code></pre>
  
  <p>Doing a quick-check by placing the '_CrtDumpMemoryLeaks()' without including the header-files confirms my suspicion that the header-files are indeed incuded through the STL files.</p>
  
  <pre><code>// The header files that should be included for using the CrtDumpMemoryLeaks:
  //#define _CRTDBG_MAP_ALLOC
  //#include &lt;stdlib.h&gt;
  //#include &lt;crtdbg.h&gt;
  //...
  _CrtDumpMemoryLeaks()
  </code></pre>
  
  <p>Leaving aside wether it's a good idea or not to implement my own new/delete, I am wondering how I can have my own new/delete implementations while still using some standard library functionality and not having these redefinition errors.</p>
  
  <hr>
  
  <p>The code looks like this:</p>
  
  <h2>memdebug.h</h2>
  
  <pre><code>#ifndef _MEM_DEBUG_H
  #define _MEM_DEBUG_H
  
  #ifdef _DEBUG
      void * operator new( unsigned int size, const char *filename, int line );
      void operator delete( void *ptr );
  
      #define DEBUG_NEW new(__FILE__, __LINE__)
      #define new DEBUG_NEW
  #endif
  
  #endif
  </code></pre>
  
  <h2>memdebug.c</h2>
  
  <pre><code>#ifdef _DEBUG
  void * operator new( unsigned int size, const char *filename, int line )
  {
      void *ptr = (void *)malloc(size);
      //AddTrack((DWORD)ptr, size, filename, line);
      return(ptr);
  };
  
  void operator delete( void *ptr )
  {
      //RemoveTrack( (DWORD)ptr );
      free( ptr );
  }
  #endif
  </code></pre>
  
  <h2>main.cpp</h2>
  
  <pre><code>#include "memdebug.h"
  #include &lt;iostream&gt;
  
  void main()
  {
      Test *pTest = new Test();
      std::cout &lt;&lt; "end" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The way I solved it is by moving the <code>#define new DEBUG_NEW</code> below the <code>&lt;iostream&gt;</code>;
  The problem is resolved, since it won't rewrite the new in the <code>crtdbg.h</code>; however this really makes it troublesome having to make sure that this is always being done after possible headers that include the <code>crtdbg.h</code> file.</p>
  
  <p>I believe this can only be resolved by using a custom name for the new operator and use that one instead. Am I correct?</p>