2016-01-03 45 views
14

當在OSX優勝美地上使用Valgrind時,我收到possibly lost: 2,064 bytes in 1 blocks的警告。有沒有解決這個問題?我使用brew安裝了valgrind。可能的內存泄漏Valgrind在OSX El Capitan

下面是如何重現

~/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) 
+0

如果你不介意任何標記我的答案正確,或發佈您的解決方案,如果它是不同的,我會很感激它:) –

+0

TBH我沒碰過Valgrind的或c幾個月,所以我沒有關於wh的知情意見在正確的。在這種情況下什麼是SO協議? – Idr

+0

不確定。我想如果你仍然在想出如何解決你的問題,或者如果你打算在未來嘗試解決一段時間,那就留下來,直到你發現某人的答案是有幫助的或者只是發佈你自己的答案。如果你只是完全放棄了這個問題,並不打算回到它,那麼我想你不妨選擇一個答案來關閉它。如果有更好的答案出來,你可以隨時更改答案 –

回答

12

Valgrind是主要用於Linux的工具,以及OSX較少支持的例子。這意味着Valgrind會在OSX上產生很多誤報。如果要抑制那些可能丟失的泄漏,請將--gen-suppressions=all(或--gen-suppressions=yes,如果要選擇並選擇一個一個報告的泄漏)選項添加到您的valgrind呼叫。這將完成的是打印文本關閉一大塊每個報告的內存泄漏,這將是這個樣子:

{ 
    <insert_a_suppression_name_here> 
    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/<username>/leak1.supp選項,該內存泄漏報告將被抑制。爲了使這更容易,你可以把東西放在~/.valgrindrc文件中。該文件可能看起來像

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

或者,如果你可以在Linux機器上測試你的代碼,而不是,你會不會擔心這一切;)

- 編輯 -

我得到了很多我的信息,從this other SO post

+0

你怎麼知道這是一個誤報? – Idr

+1

一般來說,我不知道如果你看到誤判或者沒有看到你如何判斷,但是在你的具體情況下,你沒有分配任何內存,所以假設你沒有泄漏是相當合理的任何記憶。 –

+0

只是再次看着輸出,我看到迅速的東西。奇怪的!? – Idr