crash in Desktop::Displays::getDisplayContaining while arranging displays

At some point while rearranging displays using the Display preferences in OS X, the number of displays available is apparently zero. So Desktop::Displays::getDisplayContaining() throws an assertion at the line 

​    const Display* best = &displays.getReference(0);

when the size of Array<Display> displays is 0.

[EDIT] Can reproduce on 10.8.5 and 10.9.

Thanks - I've added a workaround that should prevent any problems if that happens. Would appreciate you giving it a test if you have a way of making the error happen.

Sorry to report, it's still happening. 

It looks like a race condition. I've got a thing called getBackingLayerScale() that is being called from my renderOpenGL() method. This in turn calls getDisplayContaining(). That happens while findDisplays() is being called on the message thread, after     

oldDisplays.swapWith (displays);

and before any new displays are found. 

I guess adding a critical section is the most straightforward fix. If you can't reproduce the problem easily I can give it a try. 

You're not calling it from a background thread, are you? It was never designed to be called by anything other than the message thread!

Well sure, I'm calling it from a drawing thread. There’s nothing to warn me that these methods must be called from the message thread. If that’s the case please jassert (MessageManager::getInstance()->isThisTheMessageThread()) as you do elsewhere and I'll get the idea. 

 

Yeah, I already added some assertions to catch that earlier today ;)