Fix “double free or corruption” Crash (SIGABRT) in C
The glibc heap allocator stores metadata headers and free-list links adjacent to every `malloc()` chunk. Calling `free(p)` twice, freeing an unallocated stack/string-literal address, or writing past chunk bounds corrupts those metadata headers and triggers `SIGABRT` (`exit 134`).
Quick Fix Summary
Reset freed pointers to NULL (free(ptr); ptr = NULL;), document which module owns cleanup, and compile with -g -O1 -fsanitize=address to capture both free stack traces.
Signal & Exit Code Reference
POSIX shell statuses (128 + signal) help distinguish libc aborts (134) from page faults (139); exact numbers vary by platform.
| Signal Name | Signal # | Shell Exit Code (128 + N) | Trigger Condition & Meaning |
|---|---|---|---|
| SIGABRT | 6 | 134 | Process aborted by glibc allocator (`double free or corruption`) or failed `assert()`. |
| SIGSEGV | 11 | 139 | Invalid memory access after heap pointers were corrupted or reused. |
Why This Error Happens
Why p = NULL prevents dangling reuse cleanly
Standard C guarantees free(NULL) is a safe no-op. Setting p = NULL; right after free(p) neutralizes accidental repeat free() calls on the same variable.
Pointer aliases and realloc pitfalls
If q = p, nulling p does not clear q. Track single ownership cleanly and assign realloc() results to a temporary pointer first (void *tmp = realloc(p, sz)) so failures never leak or double-free.
Top Root Causes & Specific Code Fixes
Calling free() twice on the same pointer
Double-free corrupts allocator free-list chunks immediately.
int *p = malloc(sizeof *p);
free(p);
free(p);int *p = malloc(sizeof *p);
free(p);
p = NULL;Freeing a stack pointer or interior pointer (p + 1)
free() only accepts exact addresses returned by malloc/calloc/realloc or NULL.
int arr[4];
free(arr);int *arr = calloc(4, sizeof *arr);
free(arr);Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdlib.h>
int main(void) {
int *buf = malloc(sizeof *buf);
free(buf);
free(buf);
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *buf = malloc(sizeof *buf);
if (!buf) return 1;
*buf = 42;
printf("%d\n", *buf);
free(buf);
buf = NULL;
free(buf);
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
42Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| free(): double free detected (SIGABRT / 134) | Same heap address freed twice cleanly | -fsanitize=address | Set `ptr = NULL;` after `free(ptr);` and assign single owner |
| munmap_chunk / invalid pointer | Freeing stack variable or offset pointer `p + 1` | -Wfree-nonheap-object | Pass only exact base pointers returned by `malloc`/`calloc`/`realloc` |
Debugging Techniques and Commands
gcc -std=c17 -g -O1 -fsanitize=address main.c -o app
Prints exact line numbers for original allocation, first free, and invalid second free.
gcc -std=c17 -Wall -Wextra -Wfree-nonheap-object main.c -o app
Catches static stack/global frees at compile time.
Frequently Asked Questions
Is calling free(NULL) always valid in C?
Yes. The C standard defines `free(NULL)` as a harmless no-op, which makes `free(p); p = NULL;` a reliable idiom.
Why use a temporary pointer with realloc()?
If `realloc(p, new_size)` returns `NULL` on failure, `p` remains valid and still needs `free(p)`.
What is the difference between exit code 134 and 139?
134 indicates `SIGABRT` (such as glibc detecting heap corruption or failed asserts), while 139 indicates `SIGSEGV` invalid memory access.