Valgrind 内存泄漏可达

5

我一直遇到printfscanf语句的可达内存泄漏问题。我需要完全没有泄漏。当我运行报告时,它说我在打印语句和扫描语句处发生了可达泄漏。我的问题是如何修复这些泄漏?

以下是valgrind报告:

kidslove-MacBook:src kidslove$ valgrind --leak-check=full --show-leak-kinds=all ./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();

fopen 没有 fclose - Ôrel
调用者将关闭它。 - Mohit Jain
你可以使用write代替printf,使用read代替scanf(尽管后者需要一些工作)。 - Eregrith
@Eregrith:为什么有人想要这样做? - Oliver Charlesworth
@OliverCharlesworth 管理由 printf/scanf 分配的“仍可访问块”。 - Eregrith
显示剩余5条评论
1个回答

3

您的系统上的标准C库似乎不会释放stdio使用的缓冲区。不用担心,当程序退出时,系统会释放内存。

通常,操作系统提供的valgrind软件包包含抑制文件,告诉valgrind不报告这些预期泄漏。如果您使用非标准的valgrind软件包,禁用了标准抑制,或者在该系统上构建valgrind的人没有关注这个问题,则可能出现这种情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接