系統 - Debian - Memory - 如何使用ValGrind找出Memory Leak



參考資訊:
https://stackoverflow.com/questions/5134891/how-do-i-use-valgrind-to-find-memory-leaks

main.c

#include <stdio.h>
#include <stdlib.h>
 
void test(void)
{
    malloc(1024);
}
 
int main(int argc, char** argv)
{
    test();
    return 0;
}

編譯、執行

$ gcc main.c -o main -ggdb
$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=log.txt ./main
$ vim log.txt
    ==3025== 1,024 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==3025==    at 0x483577F: malloc (vg_replace_malloc.c:299)
    ==3025==    by 0x109142: test (main.c:6)
    ==3025==    by 0x109159: main (main.c:11)

P.S. 如果沒有debug symbol,valgrind將無法正確解析出副程式名稱