Linuxでメモリ漏洩を検出する方法

4475 ワード

Valgrindは通常、プログラムのパフォーマンスとプログラム内のメモリ漏洩エラーを分析するために使用されます.
Valgrindツールセットの概要
Valgrindには、次のツールがあります.
1、memcheck:漏洩、境界外、不正ポインタなど、プログラム内のメモリの問題をチェックします.
2、callgrind:プログラムコードの実行時間と呼び出し過程を検出し、プログラム性能を分析する.
3、cachegrind:CPUのcache命中率、損失率を分析し、コード最適化を行う.
4、helgrind:マルチスレッドプログラムの競合条件を検査するために使用する.
5、massif:スタックアナライザ、プログラムでどれだけのスタックメモリが使用されているかなどの情報を示す.
    6、lackey:
    7、nulgrind:
これらのツールの使用は、コマンド:valgrand--tool=nameプログラム名によってそれぞれ呼び出され、toolパラメータを指定しない場合、デフォルトは--tool=memcheckです.
二Valgrindツールの詳細
1.Memcheck
プログラムに発生するメモリの問題を検出するための最も一般的なツールで、すべてのメモリの読み書きが検出され、malloc、free、new、deleteの呼び出しがキャプチャされます.したがって、次の問題を検出できます.
1、初期化されていないメモリの使用;
2、読み書き解放後のメモリブロック;
3、リード/ライトがmallocに割り当てられたメモリブロックを超えている.
4、読み取り/書き込みが不適切なスタック内のメモリブロック;
5、メモリが漏れて、1つのメモリを指すポインタが永遠に失われる.
6、不正なmalloc/freeまたはnew/deleteマッチング;
7、memcpy()相関関数のdstとsrcポインタが重なる.
これらの問題はC/C++プログラマーが最も頭を悩ませる問題であり、Memcheckはここで役に立つ.例:
    #include   
    #include   
    #include   
      
    void test()  
    {  
        int *ptr = malloc(sizeof(int)*10);  
      
        ptr[10] = 7; //       
      
        memcpy(ptr +1, ptr, 5); //      
      
      
        free(ptr);   
        free(ptr);//       
      
        int *p1;  
        *p1 = 1; //       
    }  
      
    int main(void)  
    {  
        test();  
        return 0;  
    }  

プログラムコンパイル実行ファイルを生成して実行:valgrind--leak-check=full./プログラム名
出力結果は次のとおりです.
    ==4832== Memcheck, a memory error detector  
    ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.  
    ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info  
    ==4832== Command: ./tmp  
    ==4832==   
    ==4832== Invalid write of size 4      //       
    ==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd  
    ==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)  
    ==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==   
    ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) //      
    ==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)  
    ==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==   
    ==4832== Invalid free() / delete / delete[] //       
    ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
    ==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd  
    ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
    ==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==   
    ==4832== Use of uninitialised value of size 4 //       
    ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==   
    ==4832==   
    ==4832== Process terminating with default action of signal 11 (SIGSEGV) //                 
    ==4832==  Bad permissions for mapped region at address 0x419FFF4  
    ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
    ==4832==   
    ==4832== HEAP SUMMARY:  
    ==4832==     in use at exit: 0 bytes in 0 blocks  
    ==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated  
    ==4832==   
    ==4832== All heap blocks were freed -- no leaks are possible  
    ==4832==   
    ==4832== For counts of detected and suppressed errors, rerun with: -v  
    ==4832== Use --track-origins=yes to see where uninitialised values come from  
    ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)  
    Segmentation fault  

valgrindの検出出力結果から,これらのエラーはすべて見つかった.