当前位置:  开发笔记 > 运维 > 正文

OSX El Capitan中可能的内存泄漏Valgrind

如何解决《OSXElCapitan中可能的内存泄漏Valgrind》经验,为你挑选了1个好方法。

possibly lost: 2,064 bytes in 1 blocks在OSX Yosemite上使用Valgrind时收到警告.有没有解决这个问题?我用brew安装了valgrind.

以下是如何重现的示例

~/cat hello.c
int main() {
    return 123;
}

~/uname -a
Darwin mac.local 15.2.0 Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64 i386 MacBookAir6,2 Darwin

~/clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix

~/valgrind --version
  valgrind-3.11.0

~/brew info valgrind
valgrind: stable 3.11.0 (bottled), HEAD
Dynamic analysis tools (memory, debug, profiling)
http://www.valgrind.org/
/usr/local/Cellar/valgrind/3.11.0 (328 files, 46.7M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/valgrind.rb

~/clang hello.c -o hello.o

~/valgrind --leak-check=full ./hello.o
==7972== Memcheck, a memory error detector
==7972== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==7972== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==7972== Command: ./hello.o
==7972== 
==7972== 
==7972== HEAP SUMMARY:
==7972==     in use at exit: 22,411 bytes in 187 blocks
==7972==   total heap usage: 271 allocs, 84 frees, 28,651 bytes allocated
==7972== 
==7972== 2,064 bytes in 1 blocks are possibly lost in loss record 57 of 62
==7972==    at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==7972==    by 0x1004F3EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E7182: protocols() (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E7093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E4C13: gc_init (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004EC24E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004F9132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E783C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E7300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E72E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E72E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==7972==    by 0x1004E72E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==7972== 
==7972== LEAK SUMMARY:
==7972==    definitely lost: 0 bytes in 0 blocks
==7972==    indirectly lost: 0 bytes in 0 blocks
==7972==      possibly lost: 2,064 bytes in 1 blocks
==7972==    still reachable: 0 bytes in 0 blocks
==7972==         suppressed: 20,347 bytes in 186 blocks
==7972== 
==7972== For counts of detected and suppressed errors, rerun with: -v
==7972== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 17)

Ben Lindsay.. 16

Valgrind主要是Linux的工具,对OSX的支持较少.这意味着Valgrind将在OSX上产生大量误报.如果您想要抑制那些可能丢失的泄漏,那么在您的呼叫中添加--gen-suppressions=all(或者--gen-suppressions=yes如果您想逐个选择报告的泄漏)选项valgrind.这将做的是为每个报告的内存泄漏打印一大块文本,看起来像这样:

{
   
   Memcheck:Leak
   match-leak-kinds: indirect
   fun:malloc
   fun:__Balloc_D2A
   fun:__rv_alloc_D2A
   fun:__dtoa
   fun:__vfprintf
   fun:__v2printf
   fun:vfprintf_l
   fun:printf
   fun:main
}

将括号和所有内容复制并粘贴到名为的文件中/Users/username/leak1.supp.您可以随意更改<...>为抑制的实际名称.然后,当您调用时valgrind,如果添加--suppressions=/Users//leak1.supp选项,将禁止该内存泄漏报告.为了使这更容易,你可以把东西放在一个~/.valgrindrc文件中.这个文件看起来像

--tool=memcheck
--leak-check=full
--show-reachable=yes
--suppressions=/Users/benlindsay/leak1.supp
--suppressions=/Users/benlindsay/leak2.supp

或者,如果您只是在Linux机器上测试代码,则不必担心所有这些;)

- 编辑 -

我从其他SO帖子中获得了很多信息



1> Ben Lindsay..:

Valgrind主要是Linux的工具,对OSX的支持较少.这意味着Valgrind将在OSX上产生大量误报.如果您想要抑制那些可能丢失的泄漏,那么在您的呼叫中添加--gen-suppressions=all(或者--gen-suppressions=yes如果您想逐个选择报告的泄漏)选项valgrind.这将做的是为每个报告的内存泄漏打印一大块文本,看起来像这样:

{
   
   Memcheck:Leak
   match-leak-kinds: indirect
   fun:malloc
   fun:__Balloc_D2A
   fun:__rv_alloc_D2A
   fun:__dtoa
   fun:__vfprintf
   fun:__v2printf
   fun:vfprintf_l
   fun:printf
   fun:main
}

将括号和所有内容复制并粘贴到名为的文件中/Users/username/leak1.supp.您可以随意更改<...>为抑制的实际名称.然后,当您调用时valgrind,如果添加--suppressions=/Users//leak1.supp选项,将禁止该内存泄漏报告.为了使这更容易,你可以把东西放在一个~/.valgrindrc文件中.这个文件看起来像

--tool=memcheck
--leak-check=full
--show-reachable=yes
--suppressions=/Users/benlindsay/leak1.supp
--suppressions=/Users/benlindsay/leak2.supp

或者,如果您只是在Linux机器上测试代码,则不必担心所有这些;)

- 编辑 -

我从其他SO帖子中获得了很多信息

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有