1

ive got a C program that gets caught in a for loop that it shouldn't, running it with

valgrind --tool=memcheck --leak-check=yes a.out

doesnt return anything even up until the program gets caught. is there a way to change the settings of valgrind to help me find the leak? as many have pointed out, it wouldnt be considered a leak, apologies

thanks in advance

here is the loop in question

int clockstate=0;
int clocklength=0;
int datalength=0;
int datastate=0;
int dataloc = 9;

((((some other code that i don't think is important to this part))))

int dataerr[13] = {0};
int clockerr[13] = {0};     // assumes that spill does not change within an event.
int spill=0;
int k = 0;

spill = Getspill(d+4*255+1);                // get spill bit from around the middle
//printf("got spill: %d \n", spill);            // third breakpoint
for( k = 0; k < 512; k++)
{
    // Discardheader(d);      // doesnt actually do anything, since it's a header.f
    int databit = Getexpecteddata(d+4*k+1);
    printf("%d ",k);
    int transmitted = Datasample(&datastate, &datalength, d+4*k+2,dataerr,dataloc, databit);
    printf("%d ",k);
    Clocksample(&clockstate, &clocklength, d+4*k+3,clockerr, transmitted); 
    printf("%d \n",k);
    // assuming only one error per event (implying the possibility of multi-error "errors"
    // we construct the final error at the very end of the (outside this loop)


}

and the loop repeats after printing

254 254 254

255 255 255

256 256 1 <- this is the problem

2 2 2

3 3 3

edit** so i've tracked down where it is happening, and at one point in

void Clocksample (int* state, int* length, char *d, int *type, int transbit);

i have code that says *length = 1; so it seems that this command is somehow writing onto int k. my question now is, how did this happen, why isnt it changing length back to one like i want, and how do i fix it. if you want, i can post the whole code to Clocksample

swicano
  • 85
  • 1
  • 7
  • 1
    Getting stuck in a loop and having a memory leak are two different issues. – Mat Jan 06 '12 at 07:07
  • well it's a for loop and i've made very sure not to alter the index inside the loop, is there another way to get stuck in one? – swicano Jan 06 '12 at 07:08
  • Can you post the suspected portion of the code, if possible? – Bhaskar Jan 06 '12 at 07:11
  • 2
    what is the type of `k`? – Sangeeth Saravanaraj Jan 06 '12 at 07:17
  • its declared as `int k = 0` a couple lines above the loop. – swicano Jan 06 '12 at 07:20
  • 1
    possible duplicate of [need help finding why a for loop's counter variable is being altered by a function inside the loop](http://stackoverflow.com/questions/8677433/need-help-finding-why-a-for-loops-counter-variable-is-being-altered-by-a-functi) – Michael Burr Jan 06 '12 at 07:28
  • You've given us even less information than the last time you asked this question. – Michael Burr Jan 06 '12 at 07:31
  • ah, last time it was a memory error that i thought i fixed, but perhaps i am wrong, youre right. i didnt even think the two would be related. sorry. should i delete this or something? – swicano Jan 06 '12 at 07:41

3 Answers3

2

Similar to last time, something in one of those functions, Clocksample() this time, is writing to memory that doesn't belong to the data/arrays that the function should be using. Most likely an out of bounds array write. Note: this is not a memory leak, which is allocating then losing track of memory blocks that should be freed.

Set a breakpoint at the call to Clocksample() for when k is 256. Then step into Clocksample(), keeping a watch on k (or the memory used by k). You can probably also just set a hardware memory write breakpoint on the memory allocated to k. How you do any of this depends on the debugger you're using.

Now single-step (or just run to the return of Clocksample() if you have a hardware breakpoint set) and when k changes, you'll have the culprit.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • shouldnt valgrind catch that sort of thing though? under the category of illegal read write error? – swicano Jan 06 '12 at 07:44
  • 1
    I'm not all that familiar with valgrind, but I'm sure there are some bad pointer manipulations that even it can't track. – Michael Burr Jan 06 '12 at 07:50
  • thanks, i was having trouble because i didn't know how to use my debugger properly and working my way around it removed the error somehow. thanks. – swicano Jan 08 '12 at 13:11
2

Please note that Valgrind is exceedingly weak when it comes to detecting stack buffer overflows (which is what appears to be happening here).

Google address-sanitizer is much better at detecting stack overflows, and I suggest you try it instead.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
1

So your debugging output indicates that k is being changed during the call to your function Clocksample. I see that you are passing the addresses of at least two variables, &clockstate and &clocklength into that call. It seems quite likely to me that you have an array overrun or some other wild pointer in Clocksample that ends up overwriting the memory location where k is stored.

It might be possible to narrow down the bug if you post the code where k is declared (and whatever other variables are declared nearby in the same scope). For example if clocklength is declared right before k then you probably have a bug in using the pointer value &clocklength that leads to writing past the end of clocklength and corrupting k. But it's hard to know for sure without having the actual layout of variables you're using.

valgrind doesn't catch this because if, say, clocklength and k are right next to each other on the stack, valgrind can't tell if you have a perfectly valid access to k or a buggy access past the end of clocklength, since all it checks is what memory you actually access.

Roland
  • 6,227
  • 23
  • 29
  • i declare `int clockerr[13]= {0};` `int spill = 0;` `int k = 0;` so i'll give that a try, thanks a lot! – swicano Jan 06 '12 at 07:46
  • i would be able to tell if this was the case by changing where i declare `k`, right? – swicano Jan 06 '12 at 08:00
  • I changed where i declared k and it moved the error to Datasample instead, so it seems you are spot on. i then moved it even higher, and the error didnt show up anymore. it is obviously still there though. – swicano Jan 06 '12 at 08:59