with debug lines it work, without it won't

I have a problem that my program crashed quite fast aster I started it, it sent some byte to serial ports, and then it crashed, this happened when I disabled a device (to simulate non working devices) so I knew what was wrong and wanted to do some debugging at that linepart of the code and inserted a printf with a variable.

But suddenly it started working, not a single error anymore so I thought it was an coincidence and removed the debug line, but when I started it again the error was back. So I checked it a few more times, but it kept on doing that. Then I left it in and ran it from the system boot, and there the error was again, I think it has nowhere to print to so it skips the line.

But it still won’t work how it should, the only line I added is

printf("ia %d\n",ia);

Does anybody know how to fix this because I thought it is really strange. I already tried to get it to sleep there and tried the same thing with sprintf and fprintf.

The system I am running on is QNX 4.25 with Watcom 10.6 C compiler.

This is usually the sign of either a corrupted heap or stack overflow. It may happen if the variable ia is not 4 bytes in size.

I’ve also seen this if printf prior to the one you mentionned have arguments and format that don’t match.

Did you initialize your Strings or memoryregions?

It also could indicate a race condition. Slow things down with a printf and it works. Let it go at full speed and the race condition occurs. These are usually the most challenging type of bugs to find. You often have to know your code real well so you can out think it. Solutions often come at 4AM while you are sleeping.

I was thinking about the printf slowing down things too, but I also put sleep at the same place, so thought that shouldn’t be the thing. Also I know it isn’t too big as it has a maximum of 16, so it could only be higher than 16 if there is a buffer overflow of which I am quite sure didn’t happen as I would see a different value than expected when I print it. And my variables are all good initialized, I have been checking them all.

Today I also had the same problem when printing a line without any variable. I asked a colleague and he suggested me to go back to an older version, so I did and that fixed it. But still I want to know what went wrong, it was such a strange bug and when it would happen again its always better to know what to do to fix it than continueing with older code. And the bug might still be there but just doesn’t show up.

One way that sometimes work to investigate this type of problem is to create a custom logger. Your program sends messages to the logger instead of printf’s. The messages are very fast, so they are less likely to change things.