Updated:

1 minute read

개요

  • 사이트
  • Linux 프로그램 디버깅 및 프로파일링을 위한 오픈 소스 도구


설치

  • dnf install valgrind


메모리 릭

  • 소스
     #include <iostream>
        
     using namespace std;
        
     int main() {
         int *i = new int;
         *i = 1;
         cout << i << endl;
        
         return EXIT_SUCCESS;
     }
    
  • 명령어
    • valgrind --leak-check=full --show-reachable=yes --track-origins=yes --verbose --log-file=valgrind-out.txt ./main
    • --leak-check
      • --leak-check=<no|summary|yes|full> [default: summary]
      • 프로그램 종료 시 메모리 릭 검색
    • --show-reachable
      • --show-reachable=<yes|no>
      • 모든 종류의 메모리 릭 출력
    • --track-origins=yes
      • --track-origins=<yes|no> [default: no]
      • 초기화되지 않은 값을 출력
      • 검사 속도가 절반으로 줄고 메모리 사용량이 증가
    • --verbose
      • 추가 정보 제공
  • 결과
     ...
     ==514523== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
     ==514523==    at 0x4844FF5: operator new(unsigned long) (vg_replace_malloc.c:422)
     ==514523==    by 0x4011A7: main (main.cpp:6)
     ==514523==
     ==514523== LEAK SUMMARY:
     ==514523==    definitely lost: 4 bytes in 1 blocks
     ==514523==    indirectly lost: 0 bytes in 0 blocks
     ==514523==      possibly lost: 0 bytes in 0 blocks
     ==514523==    still reachable: 0 bytes in 0 blocks
     ==514523==         suppressed: 0 bytes in 0 blocks
     ...
     ==514523== 1 errors in context 2 of 3:
     ==514523== Conditional jump or move depends on uninitialised value(s)
     ==514523==    at 0x54558B7: _IO_file_xsputn@@GLIBC_2.2.5 (in /usr/lib64/libc.so.6)
     ==514523==    by 0x544A9E6: fwrite (in /usr/lib64/libc.so.6)
     ==514523==    by 0x51D9EF3: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib64/libstdc++.so.6.0.29)
     ==514523==    by 0x51DA1DB: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char) (in /usr/lib64/libstdc++.so.6.0.29)
     ==514523==    by 0x4011E3: main (main.cpp:11)
     ==514523==  Uninitialised value was created by a stack allocation
     ==514523==    at 0x401196: main (main.cpp:5)
     ...
     ==514523== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)