microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
9.74k stars 793 forks source link

'crtdbg.h' with 'mimalloc/mimalloc-override.h' #856

Closed gvanem closed 4 months ago

gvanem commented 4 months ago

I just discovered this package and tried it on a project.

Works fine with MSVC's cl and clang-cl in -MD (_RELEASE-mode). But in -MDd (_DEBUG-mode) and a #include <crtdbg.h> ahead of #include <mimalloc/mimalloc-override.h> spits out a tons of warnings like these (from clang-cl -W4 -MDd):

./externals\mimalloc/mimalloc-override.h(21,9): warning: 'malloc' macro redefined [-Wmacro-redefined]
   21 | #define malloc(n)               mi_malloc(n)
      |         ^
f:\gv\WinKit\Include\10.0.22621.0\ucrt\crtdbg.h(292,17): note: previous definition is here
  292 |         #define malloc(s)          _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
      |                 ^
In file included from <built-in>:1:
In file included from ./config.h:196:
./externals\mimalloc/mimalloc-override.h(22,9): warning: 'calloc' macro redefined [-Wmacro-redefined]
   22 | #define calloc(n,c)             mi_calloc(n,c)
      |         ^
f:\gv\WinKit\Include\10.0.22621.0\ucrt\crtdbg.h(289,17): note: previous definition is here
  289 |         #define calloc(c, s)       _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
      |                 ^
...

To squelch all this noise I have to do:

#if defined(_DEBUG)
  #include <malloc.h>
  #undef  _malloca      
  #define _CRTDBG_MAP_ALLOC
  #include <crtdbg.h>
#endif

#if defined(USE_MIMALLOC)
  #undef malloc
  #undef calloc
  #undef realloc
  #undef free
  #undef strdup
  #undef _expand
  #undef _msize
  #undef _recalloc
  #undef _strdup
  #undef _wcsdup
  #undef _mbsdup
  #undef _dupenv_s
  #undef _wdupenv_s
  #undef _aligned_malloc
  #undef _aligned_realloc
  #undef _aligned_recalloc
  #undef _aligned_msize
  #undef _aligned_free
  #undef _aligned_offset_malloc
  #undef _aligned_offset_realloc
  #undef _aligned_offset_recalloc
  #include <mimalloc/mimalloc-override.h>
#endif

in my -FI./config.h file.

So how are we supposed to use _DEBUG-mode with mimalloc? Should <crtdbg.h> be after mimalloc-override.h? And I see no test for _INC_CRTDBG.

Is combination not tested/supported? Works fine AFAICS in _DEBUG too.

BTW, I'm on Win-10, x64 and use the amalgamation of mimalloc; I.e. static.c.

daanx commented 4 months ago

When you include crtdbg.h then the CRT tries to override all allocation calls to insert its own tracking. You have to pick which one you want to override -- either the CRT standard allocator, or use mimalloc -- you cannot use both at the same time. Hope this helps.

gvanem commented 4 months ago

you cannot use both at the same time.

That was not so obvious when I saw this in types.h:

#if !defined(MI_DEBUG)
#if !defined(NDEBUG) || defined(_DEBUG)
#define MI_DEBUG 2
#else
#define MI_DEBUG 0
#endif
#endif

This defined(_DEBUG) made me think mimalloc supported -MDd too.

daanx commented 4 months ago

ah, of course you can use mimalloc with debug builds -- you just should not include <crtdbg.h> together with mimalloc-override.h as that is a header file that specifically overrides calls to malloc etc using #defines to call specific CRT allocation functions (just like mimalloc tries to do with mimalloc-override.h.

gvanem commented 4 months ago

Then it seems difficult to use mimalloc to help me find leaks; no filename / line numbers. Just as well stick to ASAN and/or _DEBUG-mode. Closing.