简体   繁体   English

解决 valgrind “可能丢失” memory 泄漏

[英]Resolving valgrind “possibly lost” memory leak

I'm having trouble interpreting and debugging the valgrind output for a C program I just finished from an online course (nand2tetris) that is now functioning as expected.我在解释和调试 C 程序的 valgrind output 时遇到问题,我刚从在线课程 (nand2tetris) 中完成,现在按预期运行。 Here is a link to the source code: https://github.com/salario/nand2tetris/blob/master/assembler.c这是源代码的链接: https://github.com/salario/nand2tetris/blob/master/assembler.c

Here is the output from valgrind showing the possibly lost memory leak message.这是来自 valgrind 的 output 显示可能丢失的 memory 泄漏消息。 I compiled using this command: cc -g -o assembler assembler.c我使用这个命令编译: cc -g -o assembler assembler.c

$ valgrind --leak-check=full ./assembler Pong.asm
==35574== Memcheck, a memory error detector
==35574== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==35574== Using Valgrind-3.16.0.GIT and LibVEX; rerun with -h for copyright info
==35574== Command: ./assembler Pong.asm
==35574== 
==35574== 
==35574== HEAP SUMMARY:
==35574==     in use at exit: 13,932 bytes in 159 blocks
==35574==   total heap usage: 1,096 allocs, 937 frees, 137,988 bytes allocated
==35574== 
==35574== 24 bytes in 1 blocks are possibly lost in loss record 6 of 37
==35574==    at 0x1002B5E51: malloc_zone_calloc (in /usr/local/Cellar/valgrind/HEAD-36f444c/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==35574==    by 0x100877541: NXMapInsert (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x100877625: _NXMapRehash(_NXMapTable*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x10087757C: NXMapInsert (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x10087723D: _mapStrHash(_NXMapTable*, void const*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x100876FFC: _getObjc2ProtocolList(header_info const*, unsigned long*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x10087596E: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1008862D0: unmap_image (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1001AE5A5: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==35574==    by 0x1001AE746: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==35574==    by 0x10044FAD0: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==35574==    by 0x100874DAF: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574== 
==35574== 264 bytes in 3 blocks are possibly lost in loss record 30 of 37
==35574==    at 0x1002B5C90: calloc (in /usr/local/Cellar/valgrind/HEAD-36f444c/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==35574==    by 0x100875049: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1008862D0: unmap_image (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1001AE5A5: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==35574==    by 0x1001AE746: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==35574==    by 0x10044FAD0: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==35574==    by 0x100874DAF: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1003D1F38: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1003DE0E7: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1002C1790: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==35574==    by 0x1001C41E2: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==35574==    by 0x1001C45ED: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==35574== 
==35574== 2,064 bytes in 1 blocks are possibly lost in loss record 36 of 37
==35574==    at 0x1002B58AA: malloc_zone_malloc (in /usr/local/Cellar/valgrind/HEAD-36f444c/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==35574==    by 0x1008770B5: getProtocol(char const*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x10087596E: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1008862D0: unmap_image (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1001AE5A5: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==35574==    by 0x1001AE746: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==35574==    by 0x10044FAD0: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==35574==    by 0x100874DAF: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1003D1F38: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1003DE0E7: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1002C1790: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==35574==    by 0x1001C41E2: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==35574== 
==35574== 2,064 bytes in 1 blocks are possibly lost in loss record 37 of 37
==35574==    at 0x1002B58AA: malloc_zone_malloc (in /usr/local/Cellar/valgrind/HEAD-36f444c/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==35574==    by 0x10087801E: NXMapRemove (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x100877EA5: search_method_list(method_list_t const*, objc_selector*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1008760DD: _getObjc2MessageRefs(header_info const*, unsigned long*) (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1008862D0: unmap_image (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1001AE5A5: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==35574==    by 0x1001AE746: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==35574==    by 0x10044FAD0: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==35574==    by 0x100874DAF: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==35574==    by 0x1003D1F38: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1003DE0E7: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==35574==    by 0x1002C1790: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==35574== 
==35574== LEAK SUMMARY:
==35574==    definitely lost: 0 bytes in 0 blocks
==35574==    indirectly lost: 0 bytes in 0 blocks
==35574==      possibly lost: 4,416 bytes in 6 blocks
==35574==    still reachable: 9,516 bytes in 153 blocks
==35574==         suppressed: 0 bytes in 0 blocks
==35574== Reachable blocks (those to which a pointer was found) are not shown.
==35574== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==35574== 
==35574== For lists of detected and suppressed errors, rerun with: -s
==35574== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 4 from 4)

I've read through the documentation for valgrind and I've tried a bunch of different changes to the source code, and can't seem to resolve these issues.我已经阅读了 valgrind 的文档,并且尝试了对源代码的一系列不同更改,但似乎无法解决这些问题。 I would be grateful if someone can point me in the right direction by taking a look at this error and the source and letting me know if you can spot the possible issue(s).如果有人可以通过查看此错误和来源并让我知道您是否可以发现可能的问题,我将不胜感激。

Additionally, I've tried different recommended valgrind flags and can't figure out how to get the output to show the line number from my source file.此外,我尝试了不同的推荐 valgrind 标志,但无法弄清楚如何让 output 显示源文件中的行号。 Also, all the possibly lost memory error details don't reference any of my functions or files, so it's particularly challenging to debug.此外,所有可能丢失的 memory 错误详细信息都没有引用我的任何函数或文件,因此调试起来特别具有挑战性。

These memory leaks are from the runtime, like /usr/lib/system/libdispatch.dylib , /usr/lib/libSystem.B.dylib , /usr/lib/dyld .这些 memory 泄漏来自运行时,例如/usr/lib/system/libdispatch.dylib/usr/lib/libSystem.B.dylib/usr/lib/dyld

I ran your program on linux ( valgrind --leak-check=full./a.out Pong.asm ):我在 linux ( valgrind --leak-check=full./a.out Pong.asm ) 上运行了你的程序:

==102140== Memcheck, a memory error detector
==102140== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==102140== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==102140== Command: ./a.out Pong.asm
==102140== 
==102140== 
==102140== HEAP SUMMARY:
==102140==     in use at exit: 0 bytes in 0 blocks
==102140==   total heap usage: 927 allocs, 927 frees, 121,200 bytes allocated
==102140== 
==102140== All heap blocks were freed -- no leaks are possible
==102140== 
==102140== For lists of detected and suppressed errors, rerun with: -s
==102140== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

There are no memory leaks in your program, only the loader/runtime leak memory.您的程序中没有 memory 泄漏,只有加载程序/运行时泄漏 memory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM