1. valgrind 란..
자세한 부분과 기능은 더 공부를 해야 알겠지만.. 아무튼 leak 검사에 유용한 툴인것 같다..
2. 설치
$ sudo apt-get install valgrind
위 명령으로 간단하게 설치..
3. 사용법
$ valgrind [옵션] [실행파일명 인자값]
Valgrind 매뉴얼 http://valgrind.org/docs/manual/
4. 사용 예시
* 임의로 작성한 leak 이 있는 코드
#include <stdio.h>
#include <stdlib.h>
#define STR_LEN 10
void do_not_free() {
void * temp = malloc(500); }
void* do_free_external( void ) {
return malloc(STR_LEN); }
int main(void) {
int i; char *s = do_free_external();
do_not_free();
for(i=0; i<STR_LEN+1; i++) {
s[i]='a'; putchar(s[i]);
}
free(s); return 0;
}
* 코드 컴파일
$ gcc -g test.c
* valgrind 수행
$ valgrind --leak-check=full ./a.out => a.out 이라는 파일로 결과를 떨어뜨린다.
* valgrind 수행 후 "a.out" 결과 파일
==4499== Command: ./a.out : ==4499== 실행한 프로세스 pid
==4499==
==4499== Invalid write of size 1
==4499== at 0x80484A8: main (test.c:14)
==4499== Address 0x4194032 is 0 bytes after a block of size 10 alloc'd
==4499== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4499== by 0x804847C: do_free_external (test.c:8)
==4499== by 0x804848C: main (test.c:11)
==4499==
==4499== Invalid read of size 1
==4499== at 0x80484B3: main (test.c:14)
==4499== Address 0x4194032 is 0 bytes after a block of size 10 alloc'd
==4499== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4499== by 0x804847C: do_free_external (test.c:8)
==4499== by 0x804848C: main (test.c:11)
==4499== HEAP SUMMARY:
==4499== in use at exit: 500 bytes in 1 blocks :leak 이 1block 에서 500 byte 발생하였다..
==4499== total heap usage: 2 allocs, 1 frees, 510 bytes allocated
==4499==
==4499== 500 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4499== at 0x4024F20: malloc (vg_replace_malloc.c:236) : leak 발생위치
==4499== by 0x8048465: do_not_free (test.c:5)
==4499== by 0x8048495: main (test.c:12)
==4499==
==4499== LEAK SUMMARY:
==4499== definitely lost: 500 bytes in 1 blocks : 고쳐야 할 부분.. leak 이 있으니깐 고쳐라..
==4499== indirectly lost: 0 bytes in 0 blocks : 포인터 기반 구조체 leak..
==4499== possibly lost: 0 bytes in 0 blocks
==4499== still reachable: 0 bytes in 0 blocks
==4499== suppressed: 0 bytes in 0 blocks
==4499==
==4499== For counts of detected and suppressed errors, rerun with: -v
==4499== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 11 from 6)