简体   繁体   中英

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. Here is a link to the source code: https://github.com/salario/nand2tetris/blob/master/assembler.c

Here is the output from valgrind showing the possibly lost memory leak message. I compiled using this command: 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. 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. Also, all the possibly lost memory error details don't reference any of my functions or files, so it's particularly challenging to debug.

These memory leaks are from the runtime, like /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 ):

==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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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