【问题标题】:Valgrind showing memory leak for printf and unused blocksValgrind 显示 printf 和未使用块的内存泄漏
【发布时间】:2014-10-16 16:40:30
【问题描述】:
#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;
}

当我在我的 Mac(OS X,10.9.5)上使用 Valgrind 时,我收到以下消息:

==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块是哪里来的?

【问题讨论】:

  • 在我的 amd64 Linux 系统上它说根本没有泄漏。

标签: c valgrind


【解决方案1】:

在 Valgrind 团队优先考虑 OS X 之前,您可以放心地假设它不会在运行高于 10.7 的 OS X 版本的 Apple 系统上给出正确的结果。

在我的 Mavericks (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 Jessie 安装没有泄漏。

【讨论】:

    【解决方案2】:

    它并没有告诉你有泄漏:

    ==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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多