The other day I ran into a code bug that took a while to figure out. It was so unique (to me) that I thought I’d write about it.
The following pseudo code contains the bug. The symptom was that mid-execution, the value of position was getting corrupted and thus causing a GPF 1 at the sprintf.
Did you spot the bug?!
I had mistakenly declared timerInterval as an int, rather than a double. And because I’m not in the habit of error-checking the CVI standard library, the call to GetAsyncTimerAttribute blindly overfilled timerInterval with its double value, which then overwrote the value of position.
position was the collateral damage because it’s declared before timerInterval.
Now curiously, when I tried to reproduce this bug on another machine, position retained its 0 initialized value. So you can see how insidious this kind of bug can be — you might not even see it on all machines or at every runtime call.