Seemingly random crashes in runDispatchLoop() on OS X

I have an application that’s based around AudioProcessors, and so far I haven’t run into any issues with cross-platform compatibility. However, I recently started using custom AudioProcessors that rely heavily on MidiBuffers, including one that copies events from the MidiBuffer passed to the process() method into a second MidiBuffer, and then draws graphics based on the contents of the second buffer.

I didn’t think there was anything inherently wrong with this, since everything runs smoothly on Linux. But on OS X (10.8, Xcode 4.6.2), including these AudioProcessors in the AudioProcessorGraph causes my application to crash randomly when starting or stopping audio callbacks. Sometimes I can toggle the callbacks dozens of times without any problems; other times it crashes on the first try. The only consistent thing is that the error always happens inside runDispatchLoop(). Common failure points include JuceMainMenuHandler::updateMenuBar() and JuceNSViewComponentPeer::drawRect(), and it almost always ends with a line of machine code.

Has anyone encountered this problem before? Are there any red flags based on what I’ve described? Any obvious solutions? So far I’ve tried adding MessageManagerLocks in various places, but to no avail.

I updated the JUCE library for my project 6 days ago. FWIW, I changed some code inside MidiBuffer::addEvent() and MidiMessage::getMessageLengthFromFirstByte() in order to send messages of longer than 3 bytes. I can include the changes if you think it might be relevant, but I’m not seeing any errors related to these lines.

Anyone have any tips for debugging this? The crashes are still happening about 10% of the time I stop the callbacks.

Well, it’ll be some kind of threading issue. Are you starting/stopping the graph from the message thread? If not, that’d probably be a good move!

It appears that I am. If I call MessageManager::isThisTheMessageThread() right before AudioDeviceManager::removeAudioCallback(), it returns ‘true’. Is there anything else I should be wary of?

Can’t think of anything else - threading bugs are the worst, there’s no magic trick to debugging them.