From 80b89cf6c8a6581245d65bc8d3fe67412c0f1bf7 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 12 Oct 2018 11:37:06 +0100 Subject: [PATCH 1/7] 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. --- stb_leakcheck.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index b8c8df1..32cf93c 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -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 // 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__) From 248604ffbcd050259024e145428d8345da1769f3 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 18 Oct 2018 13:29:55 +0100 Subject: [PATCH 2/7] Shut up GCC -pedantic warnings --- stb_leakcheck.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 32cf93c..d68e109 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -114,7 +114,7 @@ void stb_leakcheck_dumpmem(void) 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); + printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, (void*)(mi+1)); } mi = mi->next; } @@ -124,7 +124,7 @@ void stb_leakcheck_dumpmem(void) 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); + printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, (void*)(mi+1)); } mi = mi->next; } From f7d9426f8e3f56a2384a4ced1c2f051cb7699383 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:25:47 +0000 Subject: [PATCH 3/7] Add a way to use %zd on MinGW(-w64) This is suggested here: https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ Compared to using __USE_MINGW_ANSI_STDIO, this prevents those annoying warning about "unsupported" format specifiers. --- stb_leakcheck.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index d68e109..ad0938b 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -91,19 +91,23 @@ void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line) static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr) { -#if (defined(_MSC_VER) && _MSC_VER < 1900) /* 1900=VS 2015 */ || defined(__MINGW32__) +#if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015 // Compilers that use the old MS C runtime library don't have %zd // 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 */ || defined(__MINGW32__) + #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 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); #endif #else // Assume we have %zd on other targets. - printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #ifdef __MINGW32__ + __mingw_printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #else + printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #endif #endif } From 79dc50bb79bfa0caeeef62dfb8b2c4fe09926d29 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:49:17 +0000 Subject: [PATCH 4/7] ...Actually, I don't think these are meant to be here Looking at 501812f307cd4b7609a1c6226f26bb7780cf08b8, the entire point was to *replace* these lines. That explains why they lacked braces earlier. --- stb_leakcheck.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index ad0938b..9580bfa 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -116,20 +116,14 @@ 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, (void*)(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, (void*)(mi+1)); - } mi = mi->next; } #endif From 359bb10d3cfd72f9bb2b308b65b022b4a0a6c3a5 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:55:07 +0000 Subject: [PATCH 5/7] Try to clean up stblkck_internal_print a little No need to pass all those parameters. Also long longs aren't 8 digits long. I don't know how to find out the length of a size_t at compile-time in a way I can use with a format specifier. --- stb_leakcheck.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 9580bfa..9fefceb 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -89,7 +89,7 @@ void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line) } } -static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr) +static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi) { #if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015 // Compilers that use the old MS C runtime library don't have %zd @@ -97,16 +97,16 @@ static void stblkck_internal_print(const char *reason, const char *file, int li // 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 - printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr); + printf("%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1)); #else - printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr); + printf("%s: %s (%4d): %16lld bytes at %p\n", reason, mi->file, mi->line, (long long)mi->size, (void*)(mi+1)); #endif #else // Assume we have %zd on other targets. #ifdef __MINGW32__ - __mingw_printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + __mingw_printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1)); #else - printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1)); #endif #endif } @@ -116,14 +116,14 @@ 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); + stblkck_internal_print("LEAKED", mi); 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); + stblkck_internal_print("FREED ", mi); mi = mi->next; } #endif From 3e6370720e4811d5fc10fb09f1f0efd67ec04a69 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 2 Nov 2018 00:13:40 +0000 Subject: [PATCH 6/7] Fixed STB_LEAKCHECK_SHOWALL 5a5cf7f9ba936bb931ecde8df9465e250132fb75 derped --- stb_leakcheck.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 9fefceb..705c6fc 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -58,8 +58,8 @@ void stb_leakcheck_free(void *ptr) mi->prev->next = mi->next; if (mi->next) mi->next->prev = mi->prev; - #endif free(mi); + #endif } } From 610a976b83d38fb17d57bc895d57b91a1c2bb422 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 2 Nov 2018 00:20:31 +0000 Subject: [PATCH 7/7] Removed redundant check --- stb_leakcheck.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 705c6fc..45894d4 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -96,7 +96,7 @@ static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info // 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 _MSC_VER < 1400 // before VS 2005 printf("%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1)); #else printf("%s: %s (%4d): %16lld bytes at %p\n", reason, mi->file, mi->line, (long long)mi->size, (void*)(mi+1));