From 06cdc35c5a993440a0b61f6e29f6ccbf4ff608ff Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Mon, 11 May 2026 21:25:44 -0400 Subject: detect double frees --- teensy/teensy_mem.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'teensy/teensy_mem.c') diff --git a/teensy/teensy_mem.c b/teensy/teensy_mem.c index 735cf55..62088b8 100644 --- a/teensy/teensy_mem.c +++ b/teensy/teensy_mem.c @@ -144,19 +144,29 @@ void *ty_realloc(void *ptr, size_t new_size) void ty_free(void *ptr) { - free(ptr); - #ifdef TEENSY_DEBUG + bool double_free = true; + for (int i = 0; i < allocations_len; i++) { struct alloc *alloc = &allocations[i]; if (alloc->ptr == ptr) { free(alloc->backtrace); *alloc = allocations[allocations_len - 1]; allocations_len--; + double_free = false; break; } } + + if (double_free) { + ty_log_fatal( + TY_ERR_MEM, + "Double free or freed unowned memory (%p)", + ptr + ); + } #endif // TEENSY_DEBUG + free(ptr); } int ty_alloc_count(void) -- cgit v1.3-2-g0d8e