Unrelated Code Changes Cause Sections of Code to Stop Working Properly

I'm encountering a strange problem where changing a unrelated section of code causes known good code to stop working.

I can't share the actual code here, due to its proprietary nature, but will describe the general setup in case anyone has seem a similar issue or ideas about what could be going on.

My main loop checks a few inputs, talks to an I2C device, sets outputs accordingly, and then parses serial data if it is present. The parsed data is then checked and used to perform the requested function. For example, if the serial input is "STATUS", then the software creates and reports status. Everything works fine and the parsing code has been used successfully in the past.

Here's where it gets weird. If I make a specific minor change in the code before I check for and parse the serial input, the parsing no longer works. It still compiles fine, but the same serial input, e.g. "STATUS" is no longer parsed properly. The input string from the serial port that gets fed to the parser is still correct. The minor change is something like setting a variable. If I set the variable, the parser doesn't work. Comment out the line that sets the variable and the parsing works again. I can add other lines to the code and it doesn't have the problem, but when I find one that breaks the parser, it's very repeatable. That variable is completely unrelated to anything the parser does.

Has anyone experienced similar issues where adding a line in one section of code causes known good code to stop working?

I tried generalizing my code to allow it to be shared, but have yet been unable to reproduce the issue in the pared down code. I'm seeing this issue with Arduino IDE 1.8.5 and 1.8.9 targeting UNOs and Pro Minis. The issue only shows itself in hardware testing.

You have a bug in your code. If you want help, you will HAVE to show it. Either that, or post on the Psychic Programmers Forum...

Regards,
Ray L.

Seems to me that you have three choices:

  • Post your code here.

  • Post an MCVE that demonstrates the problem - this is preferable to the previous option.

  • Pay someone to fix your code for you under NDA.

mdsram:
Has anyone experienced similar issues where adding a line in one section of code causes known good code to stop working?

I think I've seen problems like that when I've made a sketch write data past the end of an array.

Guessing: the compiler may optimize away variables that you never use. Setting the variable cancels that optimization and changes what lives where in memory.

That change may have pushed you over the edge in terms of RAM usage.

Another possibility is that you are writing off the end of a buffer when you process your serial data and that you were getting away with it, but now with the rearrangement of memory, you're stepping on something that matters.

Pure conjecture of course, but funky behaviour is often caused by array over or underrun.

GypsumFantastic:
I think I've seen problems like that when I've made a sketch write data past the end of an array.

Now that's the type of helpful response I was looking for. Thank you.

mdsram:
Has anyone experienced similar issues where adding a line in one section of code causes known good code to stop working?

Yes. Most of the time it means that your code has undefined behavior and relies on it. If so, it means that your "known good code" is not really good code at all. Basically, it "worked" because you were lucky to have things in accidental balance. You change anything - everything comes crashing down.

There are great many ways to introduce undefined behavior into the code. Going out-of-bounds on an array is just one of them. Brainstorming specific reasons without seeing the code is pointless.

wildbill:
Guessing: the compiler may optimize away variables that you never use. Setting the variable cancels that optimization and changes what lives where in memory.

That change may have pushed you over the edge in terms of RAM usage.

Another possibility is that you are writing off the end of a buffer when you process your serial data and that you were getting away with it, but now with the rearrangement of memory, you're stepping on something that matters.

Pure conjecture of course, but funky behaviour is often caused by array over or underrun.

All good info and ideas. Thanks!

Montmorency:
Yes. Most of the time it means that your code has undefined behavior and relies on it. If so, it means that your “known good code” is not really good code at all. Basically, it “worked” because you were lucky to have things in accidental balance. You change anything - everything comes crashing down.

There are great many ways to introduce undefined behavior into the code. Going out-of-bounds on an array is just one of them. Brainstorming specific reasons without seeing the code is pointless.

Got it, that makes sense. Thanks. I wasn’t looking for specific reasons and all of the helpful suggestions here have put me back on the right track. Thanks to all.