2015-05-28 120 views
5

保持printfscanf語句的可達內存泄漏。我需要沒有泄漏。當我運行報告時,它說我在打印語句和掃描語句中得到了可達性泄漏。我的問題是如何修復泄漏?Valgrind內存泄漏可達

這裏是Valgrind的報告:

kidslove-的MacBook:SRC kidslove $的valgrind --leak檢查=全 - 顯示,泄漏種=所有./a.out

==6405== Memcheck, a memory error detector 
==6405== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 
==6405== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info 
==6405== Command: ./a.out 
==6405== 
Enter File Name: input2.txt 
1. Print the array sorted by street. 
2. Print the array sorted by city. 
3. Print the array sorted by state then city 
4. Print the array sorrted by zip. 
5. Quit 

--> 5 
==6405== 
==6405== HEAP SUMMARY: 
==6405==  in use at exit: 42,554 bytes in 422 blocks 
==6405== total heap usage: 513 allocs, 91 frees, 53,707 bytes allocated 
==6405== 
==6405== 4,096 bytes in 1 blocks are still reachable in loss record 77 of 78 
==6405== at 0x1000076C1: malloc (vg_replace_malloc.c:303) 
==6405== by 0x1001F1836: __smakebuf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x100206387: __swsetup (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x10022075D: __v2printf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x100220C80: __xvprintf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001F6B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001F49D7: printf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1000010B3: openFile (hw5.c:15) 
==6405== by 0x100001CD5: main (cscd240hw5.c:9) 
==6405== 
==6405== 4,096 bytes in 1 blocks are still reachable in loss record 78 of 78 
==6405== at 0x1000076C1: malloc (vg_replace_malloc.c:303) 
==6405== by 0x1001F1836: __smakebuf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001F4E99: __srefill0 (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001F4F94: __srefill (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001FC00D: __svfscanf_l (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1001F515D: scanf (in /usr/lib/system/libsystem_c.dylib) 
==6405== by 0x1000010C8: openFile (hw5.c:16) 
==6405== by 0x100001CD5: main (cscd240hw5.c:9) 
==6405== 
==6405== LEAK SUMMARY: 
==6405== definitely lost: 0 bytes in 0 blocks 
==6405== indirectly lost: 0 bytes in 0 blocks 
==6405==  possibly lost: 0 bytes in 0 blocks 
==6405== still reachable: 8,192 bytes in 2 blocks 
==6405==   suppressed: 34,362 bytes in 420 blocks 
==6405== 
==6405== For counts of detected and suppressed errors, rerun with: -v 
==6405== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15) 

部分我的程序是返回泄漏:

FILE *openFile() { 
    char name[20]; 
    FILE *fin; 

    do { 
    printf("Enter File Name: "); //line 15 leak 
    scanf("%s", name); // line 16 leak 
    fin = fopen(name, "r"); 
    }while(fin == NULL); 
    return fin; 

} 

原始呼叫之中:

fin = openFile(); 
+0

'fopen' without'fclose' –

+0

來電者將關閉它。 –

+0

@Ôrel:報告指向printf/scant。其中一個人會希望fclose在其他地方...... –

回答

3

系統上的標準C庫似乎不打擾stdio使用的釋放緩衝區。這是沒有什麼可擔心的。當您的程序退出時,系統釋放內存。

操作系統提供的valgrind包通常包含壓縮文件,告訴valgrind不會報告大量這些預期的泄漏。要麼你使用的是非標準的valgrind包,你已經禁用了標準壓制,或者在該系統上構建valgrind的人沒有打擾。