使用 valgrind 对 C 检查内存时, OS X 上永远有内存泄露问题。
同样的代码放在 Linux 上完全没有问题。
如:
int main(){
return 123;
}
valgrind 返回:
HEAP SUMMARY:
==4020== in use at exit: 22,146 bytes in 187 blocks
==4020== total heap usage: 271 allocs, 84 frees, 28,386 bytes allocated
==4020==
==4020== 2,064 bytes in 1 blocks are possibly lost in loss record 57 of 62
==4020== at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4020== by 0x1004F3EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E7182: protocols() (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E7093: readClass(objc_class, bool, bool) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E4C13: gc_init (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004EC24E: objc_initializeClassPair_internal(objc_class, char const, objc_class, objc_class) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004F9132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E783C: realizeClass(objc_class) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E7300: copySwiftV1MangledName(char const, bool) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E72E9: copySwiftV1MangledName(char const, bool) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E72E9: copySwiftV1MangledName(char const, bool) (in /usr/lib/libobjc.A.dylib)
==4020== by 0x1004E72E9: copySwiftV1MangledName(char const, bool) (in /usr/lib/libobjc.A.dylib)
==4020==
==4020== LEAK SUMMARY:
==4020== definitely lost: 0 bytes in 0 blocks
==4020== indirectly lost: 0 bytes in 0 blocks
==4020== possibly lost: 2,064 bytes in 1 blocks
==4020== still reachable: 0 bytes in 0 blocks
==4020== suppressed: 20,082 bytes in 186 blocks
==4020==
==4020== For counts of detected and suppressed errors, rerun with: -v
==4020== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 17)
按照网上找的教程,将如下文件加入到 valgrind 的执行过程中。
{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: indirect
fun:malloc
fun:Balloc_D2A
fun:rvalloc_D2A
fun:dtoa
fun:vfprintf
fun:_v2printf
fun:vfprintf_l
fun:printf
fun:main
}
摘自: http://stackoverflow.com/questions/34573039/possible-memory-leak-valgrind-in-osx-el-capitan/35592274
只是屏蔽了内存泄露的具体信息,有没有什么方法,能从根本上解决?
目的: allocs = frees