8

I need a tool which helps me to find memory leaks in a c program in a similar way valgrind does. It should figures out when a program overwrites memory it should not (e.g. by miscalculating an array index). I learned that there is the leaks utility along with the graphical instruments app.

However I think it can just find memory allocated with new (or malloc) which was not released and is not accessible anymore. Also I learned that valgrind is supposed to work on older releases (10.5 and 10.6), but I use lion (10.7).

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Nils
  • 13,319
  • 19
  • 86
  • 108
  • As written above it does not (yet) work with lion (10.7). – Nils Sep 20 '11 at 19:44
  • 1
    @Mat : Lion (10.7) is not supported yet as OP pointed out, they are working on it since june, but it doesn't seem so easy : https://bugs.kde.org/show_bug.cgi?id=275168 – Matthieu Sep 20 '11 at 19:45
  • From your link @Matthieu, it seems as though people have gotten it to work. – Michael Mior Sep 21 '11 at 00:34

2 Answers2

8

Valgrind 3.7.0 (released 5th November 2011) supports Lion. http://valgrind.org

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David H
  • 40,852
  • 12
  • 92
  • 138
4

I would use the XCode developer tool MallocDebug. You should have this installed with XCode in your /Developer folder

Alternatively, you can run your application in gdb and use the native malloc logging by running

% gdb <program name>
(gdb) set env MallocStackLoggingNoCompact 1
(gdb) run

Then, you can use /usr/bin/leaks and /usr/bin/malloc_history to find obvious leaks

Seth
  • 2,683
  • 18
  • 18
  • Tested it in a case where I accidentally multiplied 2 vars in an array access instead of adding them. w/o MallocStackLoggingNoCompact the bt output is useless, but after having set that it told me the exact line! Thx for the hint! – Nils Sep 21 '11 at 16:20
  • However I couldn't find MallocDebug in 10.7. Also shark is gone.. :( – Nils Sep 21 '11 at 16:37
  • Looks like MallocDebug has been replaced by the `Leaks` instrument in the `Instruments` application. – Seth Sep 22 '11 at 15:02