Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

valgrind mac os mem leak

Tags:

c

macos

valgrind

Today I installed valgrind on my Mac os x 10.6 and tried to test it out. And it turned out to be weird memory leaks in the system. What I did was just create simple c file that get some heap memory and immediately free it. When I ran valgrind it showed something like this

Realfrees-MacBook-Pro:C Realfree$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out 
==2621== Memcheck, a memory error detector
==2621== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2621== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==2621== Command: ./a.out
==2621== 
--2621-- ./a.out:
--2621-- dSYM directory is missing; consider using --dsymutil=yes
==2621== 
==2621== HEAP SUMMARY:
==2621==     in use at exit: 88 bytes in 1 blocks
==2621==   total heap usage: 2 allocs, 1 frees, 92 bytes allocated
==2621== 
==2621== 88 bytes in 1 blocks are still reachable in loss record 1 of 1
==2621==    at 0x100010915: malloc (vg_replace_malloc.c:236)
==2621==    by 0x1000260EB: get_or_create_key_element (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x100026008: _keymgr_get_and_lock_processwide_ptr_2 (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x100025FCF: __keymgr_initializer (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x1000245E7: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x7FFF5FC0D4FF:  ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BCEB: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BC9C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BDA5: ImageLoader::runInitializers(ImageLoader::LinkContext const&) (in /usr/lib/dyld) 
==2621==    by 0x7FFF5FC020EE: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC06980: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC016D1: dyldbootstrap::start(macho_header const*, int, char const**, long) (in /usr/lib/dyld)
==2621== 
==2621== LEAK SUMMARY:
==2621==    definitely lost: 0 bytes in 0 blocks
==2621==    indirectly lost: 0 bytes in 0 blocks
==2621==      possibly lost: 0 bytes in 0 blocks
==2621==    still reachable: 88 bytes in 1 blocks
==2621==         suppressed: 0 bytes in 0 blocks
==2621== 
==2621== For counts of detected and suppressed errors, rerun with: -v
==2621== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

where a certain amount of memory is still reachable. Any idea how to fix? or this is normal for mac

P.S. I ran valgrind with command ls -l, and it showed bunch of block that were still reachable. I'm not sure this is normal behavior on mac. below is result of command line "valgrind --tool=memcheck ls -l":

==2734== 
==2734== HEAP SUMMARY:
==2734==     in use at exit: 118,331 bytes in 52 blocks
==2734==   total heap usage: 1,253 allocs, 1,201 frees, 214,242 bytes allocated
==2734== 
==2734== LEAK SUMMARY:
==2734==    definitely lost: 0 bytes in 0 blocks
==2734==    indirectly lost: 0 bytes in 0 blocks
==2734==      possibly lost: 0 bytes in 0 blocks
==2734==    still reachable: 118,331 bytes in 52 blocks
==2734==         suppressed: 0 bytes in 0 blocks
==2734== Rerun with --leak-check=full to see details of leaked memory
==2734== 
==2734== For counts of detected and suppressed errors, rerun with: -v
==2734== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
like image 593
Ha-eun Chung Avatar asked Mar 07 '11 23:03

Ha-eun Chung


People also ask

Does Valgrind work on macOS?

x and later), ARM64/Android, X86/Android (4.0 and later), MIPS32/Android, X86/FreeBSD, AMD64/FreeBSD, X86/Darwin and AMD64/Darwin (Mac OS X 10.12). Valgrind is Open Source / Free Software, and is freely available under the GNU General Public License, version 2.

What is Valgrind memory leak?

Valgrind Memcheck is a tool that detects memory leaks and memory errors. Some of the most difficult C bugs come from mismanagement of memory: allocating the wrong size, using an uninitialized pointer, accessing memory after it was freed, overrunning a buffer, and so on.

How do you detect memory leaks?

The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version. If you leave out the #define statement, the memory leak dump will be less detailed.


2 Answers

You may need to use a valgrind suppressions file for Mac. Looks like some process-wide memory is allocated by not freed when the process exits. Try with valgrind --suppressions=<path to suppression file>.

like image 75
samplebias Avatar answered Oct 03 '22 23:10

samplebias


And for Mac OS X Lion, you should use this suppression file : https://github.com/svn2github/valgrind/blob/master/darwin11.supp

like image 31
mathieug Avatar answered Oct 03 '22 22:10

mathieug