Jun's Blog

Output, activities, memo and etc.

Install memory leak check tool: valgrind to Mac 10.9

I want to use the tool to check memory leak for C program in Mac.

I am using Mac OSX 10.9.
There is one tool "Valgrind", it is

Valgrind
http://valgrind.org/
This tool is supporting Linux and Mac OS X. But OSX is for only 10.10.

But I found one nice information to install it to Mac OSX 10.9.

This document.
https://calvinx.com/2014/05/04/valgrind-on-mac-os-x-10-9-mavericks/

And also below document is for 10.10.
http://ranf.tl/2014/11/28/valgrind-on-mac-os-x-10-10-yosemite/

Install

If you have not installed below software, install it.

$ brew install autoconf
$ brew install automake


Download https://github.com/fredericgermain/valgrind homebrew branch.

$ git clone https://github.com/fredericgermain/valgrind.git -b homebrew
$ cd valgrind/
$ git branch
* homebrew

Do not forget to install submodule.

$ git submodule init
$ git submodule update
$ ./autogen.sh

$ ./configure --version
Valgrind configure 3.10.0.SVN
...

$ ./configure --prefix=/usr/local/valgrind-3.10.0

$ make
$ make install

It was installed successfully.

$ ls /usr/local/valgrind-3.10.0/

Set symbolic link.

$ cd /usr/local
$ ln -s valgrind-3.10.0 valgrind

Add bin directory to PATH environment variable.

$ vi ~/.bashrc

PATH="${PATH}:/usr/local/valgrind/bin"

export PATH

$ . ~/.bashrc

$ valgrind --version
valgrind-3.10.0.SVN

Usage

bin/thread is my sample C compiled program.
See https://github.com/junaruga/multi_test/blob/master/src/thread_main.c

$ bin/thread
Job 1 started, sleep sec: 7.
Job 2 started, sleep sec: 9.
Job 3 started, sleep sec: 3.
Job 4 started, sleep sec: 8.
Job 3 ended.
Job 1 ended.
Job 4 ended.
Job 2 ended.

Run this program by valgrind.

$ valgrind bin/thread
==40900== Memcheck, a memory error detector
==40900== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==40900== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==40900== Command: bin/thread
==40900==
==40900== WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken.
==40900== WARNING: Expect incorrect results, assertions and crashes.
==40900== WARNING: In particular, Memcheck on 32-bit programs will fail to
==40900== WARNING: detect any errors associated with heap-allocated data.
==40900==
--40900-- bin/thread:
--40900-- dSYM directory is missing; consider using --dsymutil=yes
Job 1 started, sleep sec: 7.
Job 2 started, sleep sec: 9.
Job 3 started, sleep sec: 3.
Job 4 started, sleep sec: 8.
Job 3 ended.
Job 1 ended.
Job 4 ended.
Job 2 ended.
==40900==
==40900== HEAP SUMMARY:
==40900==     in use at exit: 29,923 bytes in 374 blocks
==40900==   total heap usage: 450 allocs, 76 frees, 35,867 bytes allocated
==40900==
==40900== LEAK SUMMARY:
==40900==    definitely lost: 0 bytes in 0 blocks
==40900==    indirectly lost: 0 bytes in 0 blocks
==40900==      possibly lost: 0 bytes in 0 blocks
==40900==    still reachable: 4,096 bytes in 1 blocks
==40900==         suppressed: 25,827 bytes in 373 blocks
==40900== Rerun with --leak-check=full to see details of leaked memory
==40900==
==40900== For counts of detected and suppressed errors, rerun with: -v
==40900== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 117 from 20)

You can see HEAP SUMMARY and LEAK SUMMARY.