On Wed, 27 Nov 2024, Pali Rohár wrote:

In Visual Studio when including crtdbg.h file with both _DEBUG and
_CRTDBG_MAP_ALLOC macros set, all standard memory allocation functions are
redefined and forwarded to their *_dbg siblings. This allows to track
memory allocation in program compiled and linked with debug version of CRT
DLL library.

Add same support also into mingw-w64. This allows to use memory leak
functionality provided by msvcrtd.dll when compiling with gcc's
-mcrtdll=msvcrtd and -D_DEBUG and -D_CRTDBG_MAP_ALLOC switches.

Note that those redefined functions needs macro guards in other header
files to prevent expadning redefined macros when declaring non-debug
functions. For example when declaring void *__cdecl malloc(size_t _Size);
in malloc.h header file, it needs to be ensured that malloc macro from
crtdbg.h #define malloc(s) _malloc_dbg(s,_NORMAL_BLOCK,__FILE__,__LINE__)
does not expad for standard malloc declaration. Same guard technique via
pragma push_macro, undef and pragma pop_macro is used by Visual Studio.
---
mingw-w64-headers/crt/corecrt_wstdlib.h  | 10 +++-
mingw-w64-headers/crt/crtdbg.h           | 44 ++++++++++++++++
mingw-w64-headers/crt/direct.h           | 33 ++++++++++++
mingw-w64-headers/crt/io.h               | 15 ++++++
mingw-w64-headers/crt/malloc.h           | 67 ++++++++++++++++++++++++
mingw-w64-headers/crt/mbstring.h         |  7 +++
mingw-w64-headers/crt/sec_api/stdlib_s.h | 10 +++-
mingw-w64-headers/crt/stdio.h            | 21 ++++++++
mingw-w64-headers/crt/stdlib.h           | 63 ++++++++++++++++++++++
mingw-w64-headers/crt/string.h           | 28 ++++++++++
mingw-w64-headers/crt/wchar.h            | 42 +++++++++++++++
11 files changed, 338 insertions(+), 2 deletions(-)

This patch also seems ok to me.

On first look, this feels like it adds a lot of noise to the headers, but I guess it's not all that bad. So I can push it if nobody else objects to it.

// Martin

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to