2014-10-16 39 views
6
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]){ 
    char *str = malloc(sizeof(char)*5); 
    str = strcpy(str, "test"); 
    printf("%s\n", str); 
    free(str); 
    return 0; 
} 

當我使用Valgrind的在我的Mac內存泄漏(OS X,10.9.5)我得到以下信息:Valgrind的顯示了printf和未使用的塊

==77215== HEAP SUMMARY: 
==77215==  in use at exit: 29,211 bytes in 374 blocks 
==77215== total heap usage: 451 allocs, 77 frees, 35,160 bytes allocated 
==77215== 
==77215== 4,096 bytes in 1 blocks are still reachable in loss record 76 of 76 
==77215== at 0x66CB: malloc (in /usr/local/Cellar/valgrind/3.10.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) 
==77215== by 0x182855: __smakebuf (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x197217: __swsetup (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x1B0158: __v2printf (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x1B06AF: __xvprintf (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x187B29: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x18596F: printf (in /usr/lib/system/libsystem_c.dylib) 
==77215== by 0x100000F2B: main (test.c:8) 
==77215== 
==77215== LEAK SUMMARY: 
==77215== definitely lost: 0 bytes in 0 blocks 
==77215== indirectly lost: 0 bytes in 0 blocks 
==77215==  possibly lost: 0 bytes in 0 blocks 
==77215== still reachable: 4,096 bytes in 1 blocks 
==77215==   suppressed: 25,115 bytes in 373 blocks 
==77215== 
==77215== For counts of detected and suppressed errors, rerun with: -v 
==77215== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15) 

是否printf本身分配內存?如果我刪除printf我只得到如下:

==77237== HEAP SUMMARY: 
==77237==  in use at exit: 25,115 bytes in 373 blocks 
==77237== total heap usage: 450 allocs, 77 frees, 31,064 bytes allocated 
==77237== 
==77237== LEAK SUMMARY: 
==77237== definitely lost: 0 bytes in 0 blocks 
==77237== indirectly lost: 0 bytes in 0 blocks 
==77237==  possibly lost: 0 bytes in 0 blocks 
==77237== still reachable: 0 bytes in 0 blocks 
==77237==   suppressed: 25,115 bytes in 373 blocks 
==77237== 
==77237== For counts of detected and suppressed errors, rerun with: -v 
==77237== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15) 

哪裏了373塊從何而來?

+2

在我的AMD64的Linux系統,它說沒有泄漏的。 – Jay 2014-10-16 16:45:15

回答

4

直到Valgrind的團隊優先OS X,你可以安全地假設它不會給蘋果系統正確運行的結果比10.7更新OS X的版本。

在我的小牛(10.9.5)的機器,我還是得到Valgrind的以下警告(3.9.0)

WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken. 
WARNING: Expect incorrect results, assertions and crashes. 
WARNING: In particular, Memcheck on 32-bit programs will fail to 
WARNING: detect any errors associated with heap-allocated data. 

對於它的價值,Valgrind的3.10.0顯示了我的Debian傑西無泄漏安裝。

0

它不會告訴你,有一個泄漏:

==77215== LEAK SUMMARY: 
==77215== definitely lost: 0 bytes in 0 blocks 
==77215== indirectly lost: 0 bytes in 0 blocks 
==77215==  possibly lost: 0 bytes in 0 blocks 
==77215== still reachable: 4,096 bytes in 1 blocks 
==77215==   suppressed: 25,115 bytes in 373 blocks 

它告訴你,有一個塊,仍然可以到達;是否存在泄漏取決於您對「泄漏」的定義。它的意思是有一個指向塊的指針。

參考Still Reachable Leak detected by Valgrind以獲取更多信息。