Runtime (SIGSEGV / Signal)Runtime Signals7 min read

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 NameSignal #Shell Exit Code (128 + N)Trigger Condition & Meaning
SIGABRT6134Process aborted by glibc allocator (`double free or corruption`) or failed `assert()`.
SIGSEGV11139Invalid 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.

Broken Pattern
int *p = malloc(sizeof *p);
free(p);
free(p);
Corrected Pattern
int *p = malloc(sizeof *p);
free(p);
p = NULL;
Illustrative diagnostic (not executed):free(): double free detected in tcache 2 — Aborted (SIGABRT)

Freeing a stack pointer or interior pointer (p + 1)

free() only accepts exact addresses returned by malloc/calloc/realloc or NULL.

Broken Pattern
int arr[4];
free(arr);
Corrected Pattern
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 compiler
Read 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 compiler

Expected output from the corrected program

Reference output, not a live execution result.

42

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
free(): double free detected (SIGABRT / 134)Same heap address freed twice cleanly-fsanitize=addressSet `ptr = NULL;` after `free(ptr);` and assign single owner
munmap_chunk / invalid pointerFreeing stack variable or offset pointer `p + 1`-Wfree-nonheap-objectPass only exact base pointers returned by `malloc`/`calloc`/`realloc`

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

AddressSanitizer Double-Free Trace
gcc -std=c17 -g -O1 -fsanitize=address main.c -o app

Prints exact line numbers for original allocation, first free, and invalid second free.

Check NULL Ownership
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.