diff options
| author | iamcheeseman <[email protected]> | 2026-05-11 21:25:44 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-11 21:25:44 -0400 |
| commit | 06cdc35c5a993440a0b61f6e29f6ccbf4ff608ff (patch) | |
| tree | d66e9a94f0b9e3206058dc3cbf291eb400d342ff | |
| parent | 4607ee516f7b2653da59f5c67c39247a4b1b0820 (diff) | |
detect double frees
| -rw-r--r-- | teensy/teensy_mem.c | 14 |
1 files changed, 12 insertions, 2 deletions
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) |
