Hard to track bugs...

Started by
2 comments, last by aregee 10 years ago

I have just spent two days tracking down a mysterious bug, only to find something really stupid...

I have made a flac (Free Lossless Audio Codec) decoder from scratch, and it exhibited some weird behaviour. Some songs played completely through without any errors at all. I even compared side by side with the original wav file while playing. Other songs erred all the way through the song. There were no in-betweens. Either the song played correctly all the way, or it didn't play at all. The errors I had when the playroutine failed was of a missing sync problem, indicating a bug that caused the wrong number of bits from a block to be read. The mystery was why and where it happened. I was almost confident my decoding routine was ok, since it was going through the same stages with the songs that worked, than the ones that didn't work.

In pure desperation I tracked down a flac frame that seemed broken, and started to decode the frame manually to see what was supposed to happen. What I found was that the flac was supposed to be decoded using the LPC decoder stage, but my audio player said that it was trying to squeeze the data through the FIXED decoder stage. Finally a hint of what could be the problem. Looking at the bit pattern that chooses which stage to decode in:

001xxx = FIXED (xxx = order)

1xxxxx = LPC (xxxxx - 1 = order)

Looking at my code, i found something like this:


            if ((subFrameType & 0x08) == 0x08) {
                [frameInfo setCurrentSampleSize:[self sampleSizeForChannel:audioChannel]];
                isOK = [self subFrameFixed:audioChannel predictorOrder:(subFrameType & 0x07) fullyDecode:YES];
            }
           else if ((subFrameType &0x20) == 0x20) {
                [frameInfo setCurrentSampleSize:[self sampleSizeForChannel:audioChannel]];
                isOK = [self subFrameLPC:audioChannel lpcOrder:((subFrameType & 0x1f) + 1) fullyDecode:YES];
            }
Say we have a LPC frame with order 9, which codes to bits 101000, it will match the FIXED decoding stage because I am testing in the wrong order. If I swap the if-tests above, everything will work fine. Simple stupid error, hard to track down.
This made me wonder, what kind of simple, but hard to find bugs have you been struggling with in the past?
EDIT: It is also a story about not postponing doing the dirty work when it really is needed... If I actually did the manual decoding two days earlier that I found being too much work, I would probably have solved this problem much sooner.
Advertisement

Took me several hours to track down what ended up being something like this:


for(int i = 0; i < something; i++)
{
    for(int j = 0; i < somethingElse; j++)
        {
            //do stuff

Felt like a complete moron after that, both for writing it and for not finding it immediately.

And then there was the time I wrote the collision system for my 2D platformer while running a fever and very late at night. Led to quite a few cases of things that looked great and even almost worked, but that upon closer inspection turned out to be nonsense. Spent several nights cleaning that mess up.


function startPlayback(currentPlayerId) {

	var playButtons;
	var pauseButtons;
	var currentPlayer;
	var currentPosition;

	currentPlayer = document.getElementById(currentPlayerId);
	currentPosition = playerPositionMap[currentPlayerId];

	playButtons = currentPlayer.getElementsByClassName('play');
	pauseButtons =  currentPlayer.getElementsByClassName('pause');

	playButtons[0].innerHTML = 'Running ...';
	pauseButtons[0].innerHTML = 'Pause';

	playerIsRunningMap[currentPlayerId] = true;
	playerIntervalMap[currentPlayer.id] = setInterval(function() {
				updatePosition(currentPlayer, 10);
			}, 10
	);
}

function stopPlayback(currentPlayerId) {

	var playButtons;
	var currentPlayer;

	currentPlayer = document.getElementById(currentPlayerId);

	playButtons = currentPlayer.getElementsByClassName('play');
	pauseButtons = currentPlayer.getElementsByClassName('pause');

	playButtons[0].innerHTML = 'Play';
	pauseButtons[0].innerHTML = 'Paused ...';

	playerIsRunningMap[currentPlayerId] = false;
	clearInterval(playerIntervalMap[currentPlayer.id]);
}

Took me a while to find ... but probably just because I did too many things too fast. Pretty easy to find compared to the other examples.

My player sometimes played text faster than it should have ... and stopping didn't work any more. Took a while to realize that the play button can be pressed more often than once (no guard) and then updating suddenly happens more than once. Don't even know why I clicked more than once ever ... it seemed like random behaviour at the time.

Isn't this something for the "Coding Horrors" forum, btw.?

Here is a link: http://procgames.com/playtexts/index.html

Given enough eyeballs, all mysteries are shallow.

MeAndVR

Took me several hours to track down what ended up being something like this:


for(int i = 0; i < something; i++)
{
    for(int j = 0; i < somethingElse; j++)
        {
            //do stuff

Felt like a complete moron after that, both for writing it and for not finding it immediately.

And then there was the time I wrote the collision system for my 2D platformer while running a fever and very late at night. Led to quite a few cases of things that looked great and even almost worked, but that upon closer inspection turned out to be nonsense. Spent several nights cleaning that mess up.

Haha... I recognise that one lol. :D

Took me a while to find ... but probably just because I did too many things too fast. Pretty easy to find compared to the other examples.

My player sometimes played text faster than it should have ... and stopping didn't work any more. Took a while to realize that the play button can be pressed more often than once (no guard) and then updating suddenly happens more than once. Don't even know why I clicked more than once ever ... it seemed like random behaviour at the time.

Isn't this something for the "Coding Horrors" forum, btw.?

Here is a link: http://procgames.com/playtexts/index.html

Yes, maybe something for Coding Horrors. I was also thinking about the "bad code" section here too, but figured it wasn't really bad code, just buggy code. Oh by the way... Seems like I got a buggy build of Chrome Canary again today. Text is displaced, and I have to mark a selection to find where your link really is.

I like your text player. Kind of comfortable to read, but that is maybe not the point? On my version of Chrome I got an error when I clicked pause: "Undefined 149". It did repeat itself, so I had to check the box that prevents the JavaScript to present any more dialog boxes. My Chrome version is: Version 35.0.1911.0 canary

This topic is closed to new replies.

Advertisement