Valgrind 内存泄漏可达 [英] Valgrind Memory Leak Reachable

查看:31
本文介绍了Valgrind 内存泄漏可达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不断获取 printfscanf 语句的可访问内存泄漏.我需要完全没有泄漏.当我运行报告时,它说我在打印语句和扫描语句中获得了可到达的泄漏.我的问题是如何修复泄漏?

Keep getting reachable memory leak for printf and scanf statement. I'm needing to have no leaks at all. When I run the report it says that I get the reachable leaks at a print statement and scan statement. My question is how to I fix the leaks?

这是 valgrind 报告:

Here is the valgrind report:

kidslove-MacBook:src kidslove$ valgrind --leak-check=full --show-leak-kinds=all ./a.out

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)

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

Part of my program that is returning the leaks:

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();

推荐答案

您系统上的标准 C 库似乎并不需要释放 stdio 使用的缓冲区.这没什么好担心的.当您的程序退出时,系统会释放内存.

The standard C library on your system doesn't seem to bother freeing buffers that stdio uses. This is nothing to worry about. The system frees the memory when your program exits.

操作系统提供的 valgrind 包通常包含抑制文件,这些文件告诉 valgrind 不要报告大量预期的泄漏.要么您使用的是非标准的 valgrind 包,要么禁用了标准抑制,要么在该系统上构建 valgrind 的人都没有打扰.

Usually packages for valgrind provided by the operating system contain suppression files that tell valgrind to not report lots of those expected leaks. Either you're using a non-standard package of valgrind, you've disabled the standard suppressions or whoever built valgrind on that system didn't bother.

这篇关于Valgrind 内存泄漏可达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆