Bug Smash: The Double Bug Phenomenon

If you've done any coding, you'll almost certainly have encountered the Double Bug phenomenon.

This is where you see a bug, track it down, fix it - and either right then or a few hours or days later the exact same symptoms will recur. So you go digging and find a second bug that causes the exact same problem as the one you just fixed.

Case in point: a couple of weeks ago I fixed an edge case in Davenport where a note could become invisible. For various boring reasons, rather than editing the actual note you've selected, Davenport switches off the note's text and positions a single, re-usable note editor over the top. The edge case was that if you scrolled the mouse wheel before clicking off the note, Unity wouldn't send the message indicating editing was finished, so the note's text wouldn't get switched back on.

This took a long time to track down because the bug wasn't even in my code, but in the TextMeshPro plugin that ships with Unity (I let them know and they've fixed it for the most recent release).

Fast forward to today when I'm working on my novel - and a note vanishes when I finish editing it. I edit another note and the first reappears while the second vanishes. Exactly the same symptoms as the other bug.

So I booted up Unity and messed around for twenty minutes trying to get the problem to recur. Eventually it did, so I set up some breakpoints in code to verify that the text wasn't being re-enabled after editing.

Except it was. 

Huh.

Then I noticed that if I paused the app in Unity, the text appeared. This was a big clue because pausing the app makes all the custom geometry I create for note backgrounds vanish. So the problem was not that the text wasn't being rendered, but that it was being rendered at the wrong depth (slightly behind the note background instead of on it). Which is strange because the exact same number is passed to both the note background and the text.

A bit more stepping through revealed the culprit: in Unity when you set an object's position, it does a bit of reverse-engineering to figure out where it is in relation to its parent. And thanks to sloppy coding elsewhere (this time definitely my fault) the parent of the text object was at a different depth to the parent of the note backgrounds. Most of the time this worked out okay, but at just the right depth (because floating-point numbers don't have unlimited accuracy) the two elements ended up at very very slightly different depths - enough for one to become hidden behind the other.

Two completely unrelated bugs causing the exact same visual result. I wonder if there'll be three...

Comments

Popular Posts