Fixes for stb_leakcheck.h

I ran into a couple of problems while trying to use this with my
program:

My compiler (MinGW-w64 GCC 8.2.0) complained about not recognising %lld
format specifiers.

The compiler also warned about non-guarding 'if's. I ignored it at
first, but then I noticed my program crashed when it ran
stb_leakcheck_dumpmem. Making the 'if's guard fixed that.

After that, the lib worked until I changed how it was included: instead
of tacking a debug-only #include at the start of every file, I switched
to using GCC's '-include' option to force-include it in every file. But
doing that gave me errors about size_t not being defined. And after
fixing that, I instead got errors about the #defines messing with
stdlib.h. So to fix those I made the declaration-only part of the header
include stdlib.h.
This commit is contained in:
Clownacy 2018-10-12 11:37:06 +01:00
parent e6afb9cbae
commit 80b89cf6c8
1 changed files with 7 additions and 1 deletions

View File

@ -96,7 +96,7 @@ static void stblkck_internal_print(const char *reason, const char *file, int li
// and the older ones don't even have %lld either... however, the old compilers
// without "long long" don't support 64-bit targets either, so here's the
// compromise:
#if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005
#if (defined(_MSC_VER) && _MSC_VER < 1400) /* before VS 2005 */ || defined(__MINGW32__)
printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr);
#else
printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr);
@ -112,16 +112,20 @@ void stb_leakcheck_dumpmem(void)
stb_leakcheck_malloc_info *mi = mi_head;
while (mi) {
if ((ptrdiff_t) mi->size >= 0)
{
stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1);
printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1);
}
mi = mi->next;
}
#ifdef STB_LEAKCHECK_SHOWALL
mi = mi_head;
while (mi) {
if ((ptrdiff_t) mi->size < 0)
{
stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1);
printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1);
}
mi = mi->next;
}
#endif
@ -131,6 +135,8 @@ void stb_leakcheck_dumpmem(void)
#ifndef INCLUDE_STB_LEAKCHECK_H
#define INCLUDE_STB_LEAKCHECK_H
#include <stdlib.h> // we want to define the macros *after* stdlib to avoid a slew of errors
#define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)
#define free(p) stb_leakcheck_free(p)
#define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)