上級 › free(): double free detected in tcache の原因と直し方 › パターン1
free(): double free detected in tcache の原因と直し方
同じポインタに対してfree()を2回呼び出したときに検出されるエラーです。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) { 5 int *p = malloc(sizeof(int)); 6 *p = 1; 7 free(p); 8 ________________________ ^ 9 return 0; 10 }
free(): double free detected in tcache 2
Aborted
空欄を埋めてコンパイル・実行を通す
同じポインタに対してfree()を2回呼ぶと、メモリ管理システムが不整合を検知して強制終了します。解放したら即座にNULLを代入しておけば、誤って二重に解放しても検出しやすくなります(NULLに対するfree()は何もしない安全な操作です)。
次の問題