After spending half the day trying to figure out why my application was crashing randomly while processing the accelerometer data, I fixed it at 00:00 on the dot.
I kept getting random crashes once the callback for accelerometer data receipt was fired a few dozen times. The errors I was getting were so totally random I knew it had to be a memory trashing problem, but I couldn’t figure out where it was. All my code looked great, but of course, I wasn’t fully taking multithreading into account, and I’m multithreading this thing like you can’t imagine.
All the callback was doing was drawing a vector to the screen… how could that possibly cause a problem? Well it suddenly hit me that I was using the Ogre scene-manager to draw the vector, and since Ogre’s main loop was working away in another thread, it was very likely that I was mucking things up by throwing in a new line without waiting my turn on the scene manager data.
To fix it, I created a mutex, locked the Ogre world in the frameStarted() callback and unlocked in the frameEnded() callback. I also locked and unlocked the mutex within the scope of my accelerometerUpdated() callback. And it works!
It was horrible trying to fix such an tricky bug for so long but getting it solved is really rewarding. In fact, to celebrate, I’m going to watch more B’z videos. Here’s OH! GIRL. This involves the guitarist playing back-to-back with a chick playing a Keytar:
