I programmed a lot, but not very much in C, and I need some advice on debugging. I have a static variable (file area) that crashes after 10-100 seconds of running a multithreaded program (using pthreads in OS X 10.4). My code looks something like this:
static float some_values[SIZE];
static int * addr;
addrpoints to a valid memory address for a while, and then gets clobbered with some value (sometimes 0, sometimes other than zero), thereby causing segfault when dereferenced. With the help of gdbI checked that it is addrlaid out in memory immediately after some_values, as expected, so I assume that I used an index outside the borders to write to some_values. However, this is a tiny file, so it’s easy to verify that this is not a problem.
An obvious debugging technique would be to set the watchpoint on a variable addr. But this seems to create erratic and inexplicable behavior in gdb. The observation point is triggered upon first appointment addr; then after I continue execution, I immediately get a pointless segfault in another thread ... presumably, segfault to access the address of a static variable in another part of the program! But then gdbit allows me to read and write to this 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)
gdbobviously confusing. Does anyone know why? Or does anyone have suggestions for debugging this error without using watchpoints?
Aaron b
source
share