[Pythonmac-SIG] malloc debugger for OS X?

Jason Petrone jpetrone@cnri.reston.va.us
Tue, 8 Apr 2003 13:13:59 -0400


On Tue, Apr 08, 2003 at 11:30:54AM -0500, Skip Montanaro wrote:
> 
>     >> Can anyone point me to a malloc debugger that works under OS X and
>     >> can be used for tracking down 'double free' errors in Python C
>     >> extensions?
> 
>     Jason> I haven't tried using any of these on OS X, but I've used them on
>     Jason> other unix-like platforms:
> 
>     Jason> valgrind
>     Jason> dmalloc
>     Jason> efence
> 
>     Jason> With the possible exception of valgrind, they shouldn't have any
>     Jason> problems on OS X.
> 
> None of the above three is available via fink:
> 
> I would have expected them to turn up there by now if porting was no
> problem.

You would think so.  People must like hotline clients and games more
than malloc debuggers.

Anyway, I just tested electric fence and it works:

------------------ test.c --------------
#include <stdlib.h>

int main(int argc, char **argv) {
  void *a = malloc(1);
  free(a);
  free(a);
  return 0;
}
----------------------------------------

% gcc -g test.c -L . -lefence

% gdb a.out 
(gdb) r
Starting program: /Users/jason/Desktop/downloads/ElectricFence-2.1/a.out 
[Switching to process 22252 thread 0xb03]
Reading symbols for shared libraries . done
Reading symbols for shared libraries .. done

  Electric Fence 2.0.5 Copyright (C) 1987-1998 Bruce Perens.

ElectricFence Aborting: free(4cffc): address not from malloc().

(gdb) bt
#0  0x9001b50c in kill ()
#1  0x00003680 in EF_Abort (pattern=0x3ec8 "free(%a): address not from
malloc().") at print.c:137
#2  0x00002da4 in free (address=0x4cffc) at efence.c:632
#3  0x00001f20 in main (argc=1, argv=0xbffffd24) at test.c:6
#4  0x00001c90 in _start (argc=1, argv=0xbffffd24, envp=0xbffffd2c) at
/SourceCache/Csu/Csu-45/crt.c:267
#5  0x00001b10 in start ()
------------------------------------------

... and it finds the double free in test.c line 6

I did have to make a small change to the file page.c to match OSX
stdio.h and manually run ranlib on efence.a.

dmalloc should work as well.  it gives much more useful output, though
is slightly more work to get up and going with than electric fence.

jason