I've done a lot of programming but not much in C, and I need advice on debugging. I have a static variable (file scope) that is being clobbered after about 10-100 seconds of execution of a multithreaded program (using pthreads on OS X 10.4). My code looks something like this:
static float some_values[SIZE];
static int * addr;
addr
points to valid memory address for a while, and then gets clobbered with some value (sometimes 0, sometimes nonzero), thereby causing a segfault when dereferenced. Poking around with gdb
I have verified that addr
is being layed out in memory immediately after some_values
as one would expect, so my first guess would be that I have used an out-of-bounds index to write to some_values
. However, this is a tiny file, so it is easy to check this is not the problem.
The obvious debugging technique would be to set a watchpoint on the variable addr
. But doing so seems to create erratic and inexplicable behavior in gdb
. The watchpoint gets triggered at the first assignment to addr
; then after I continue execution, I immediately get a nonsensical segfault in another thread...supposedly a segfault on accessing the address of a static variable in a different part of the program! But then gdb
lets me read from and write to that memory address interactively.
Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x001d5bd0 0x0000678d in receive (arg=0x0) at mainloop.c:39 39 sample_buf_cleared ++; (gdb) p &sample_buf_cleared $17 = (int *) 0x1d5bd0 (gdb) p sample_buf_cleared $18 = 1 (gdb) set sample_buf_cleared = 2 (gdb)
gdb
is obviously confused. Does anyone know why? Or does anyone have any suggestions for debugging this bug without using watchpoints?