NSSpeechSynthesizer, the Cocoa class responsible for giving applications a voice under OS X, is leaky. Creating and destroying thousands of instances of this class slowly consumes all the available memory in a system, leading to degrading performance and eventual application instability.

The disappointing part is that this bug was first noticed almost two years ago.

While the extent isn’t nearly as bad, it’s still not clean.

This bug came up in our Speech Synthesizer patch for Quartz Composer, featured at kineme. After much testing, profiling, and refactoring, a splendidly small demonstration program emerged.

This program endlessly allocates an NSAutoreleasePool, used for memory management, and then an NSSpeechSynthesizer object. After creating them, it destroys them. Destroying the synthesizer object should free any associated memory with the object, and releasing the pool should free up any odds and ends left over, just in case.

It starts off looking ok. It spins for about 15 seconds or so, but then trouble strikes. As you an see, the class starts to have difficulty mapping a voice file (this is only mentioned once in all of Google’s wisdom, with no useful insight as to what it means), and eventually the inevitable crash occurs.