AVSpeechSynthesizer CRASH with certain voices under iOS 14.2+

Starting with iOS 14.2, AVSpeechSynthesizer will crash when it has a delegate assigned which implements the method, speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:).

The mere presence of the above method in the delegate will cause a crash BUT ONLY WITH CERTAIN VOICES. For example the default voice crashes while the enhanced voice, Alex does not.

I've created a simple project (which I've submitted through Feedback Assistant) with two buttons which demonstrates how one voice will cause a crash while another will not.

This breaks critical functionality of my app and my customers are not happy.

Any help with this would be much appreciated!


I made an interesting observation. 

I write a simple app. Basically it do this:
Code Block Swift
let voices = AVSpeechSynthesisVoice.speechVoices()
for voice in voices{
   let utterance = AVSpeechUtterance(string: "Hi")
   utterance.voice = voice
   synthesizer.speak(utterance)
}


When I run it I observe various phenomena. Some voices simply works, some crashed, some throws epic warnings:

Code Block
Siri TTS for certain Indian celebs isn't correct
`
And one give very interesting message:
Code Block
[AXTTSCommon] Error (hex)80000004 at /Library/Caches/com.apple.xbs/Sources/TextToSpeech/TextToSpeech-421.5/TextToSpeechBundleSupport/TTSVocalizer.m:264 (loading resource) (file:///private/var/MobileAsset/AssetsV2/com_apple_MobileAsset_VoiceServices_VoiceResources/a90d1e0d5bdcf4e8df75585d4843cd6006ba7cf3.asset/AssetData/vocalizer-user-rules-compact.dat)

I analyzed this comappleMobileAssetVoiceServicesVoiceResources directory and find it very interesting.  It contains mostly pronunciation dictionaries, for example: 
Code Block
/\bMelania(?= Trump)\b/   -->   "\e/+m$.'lA.ni.^\e/+"
/\bJoss Whedon\b/   -->   "\e/+'d&ZA+s_'wi.dn%)\e/+"
/\bJean-Claude Van Damme\b/   -->   "\e/+'ZAn_'klOd_'v@n_'d@m\e/+"

And one very interesting file comappleMobileAssetVoiceServicesVoiceResources.xml, this file exist also at URL https://mesu.apple.com/assets/com_apple_MobileAsset_VoiceServices_VoiceResources/com_apple_MobileAsset_VoiceServices_VoiceResources.xml.

This file contains entries like this:

Code Block xml
<key>_CompressionAlgorithm</key>
<string>zip</string>
<key>_DownloadSize</key>
<integer>83658</integer>
<key>_IsZipStreamable</key>
<true/>
<key>_MeasurementAlgorithm</key>
<string>SHA-1</string>
<key>_UnarchivedSize</key>
<integer>210944</integer>
<key>__BaseURL</key>
<string>http://updates-http.cdn-apple.com/2020/mobileassets/001-72580/C130C5C5-9C87-42F9-90BC-83CB9BE97872/</string>
<key>lastTimeChecked</key>
<date>2021-01-20T23:48:18Z</date>

So, for example, If they find some Indian celebrity they forgot about then they don't have to do an iOS upgrade, they simply modify VoiceResources.xml on the server and it will soon or later propagate for all iOS devices on the planet.

My hypothesis is that the latest update contains some bug. That might even be right because the crashes started on January 19 and that's also the date the mesu.apple.com returns for  VoiceResources.xml (Last-Modified: Tue, 19 Jan 2021 19:28:19 GMT). If my hypothesis is correct the fix should be rather simple and should appear within a few days. 

Please Mr. Frameworks Engineer  very much, correct this, our customers will devour us if it lasts more than a few days!








We are seeing this in our app too. We've had almost 3000 crashes since the 19th and this is totally blowing up our crash rate. (It is normally much, much lower.)

We can't just remove the willSpeakRangeOfSpeechString callback, as we use this for highlighting the text as it is read.

I filed FB8978383 on Friday. Please, please fix whatever resource file you broke, Apple.
I see that a lot of you are saying this happens only on certain voices. I only use one voice which is com.apple.speech.voice.Alex. And this crash occurs randomly (sometimes it works, sometimes it doesn't) in no particular cadence.

Relevant Stack Trace:
Code Block
OS Version: iOS 14.3 (18C66)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: BUS_NOOP at 0x0000000119b00000
Crashed Thread: 34
Application Specific Information:
appendBytes:length: > appendData: > containsString: > countByEnumeratingWithState:objects:count: > dataUsingEncoding: > JBOs > path >
Attempted to dereference garbage pointer 0x119b00000.
Thread 34 Crashed:
0 TextToSpeech 0x35cef4e38 TTSCreateDecryptedDataFromEncryptedData
1 TextToSpeech 0x35cef4e98 TTSCreateDecryptedDataFromEncryptedData
2 TextToSpeechBundleSupport 0x36ec48670 -[TTSVocalizer loadResource:mimeType:resourceURL:supportsAccurateWordCallbacks:]
3 TextToSpeechBundleSupport 0x36ec45a30 -[TTSSpeechServerInstance _loadVoiceResource:voiceResource:]
4 TextToSpeechBundleSupport 0x36ec4550c -[TTSSpeechServerInstance _initializeSpeechEngine:]
5 TextToSpeechBundleSupport 0x36ec45790 -[TTSSpeechServerInstance _initializeSpeech:]
6 TextToSpeechBundleSupport 0x36ec42c0c -[TTSSpeechServerInstance _processCurrentRequest:]
7 TextToSpeechBundleSupport 0x36ec43e40 -[TTSSpeechServerInstance _handleSpeechThread]
8 TextToSpeechBundleSupport 0x36ec42bbc _SpeechThread
9 libsystem_pthread.dylib 0x3b3779b3c _pthread_start


@ninjz, your stack trace indicates that while you requested Alex, it was not the actual voice used. Alex is not a preinstalled voice, so if the user does not have Alex installed, we will fallback to something preinstalled, which is normally Samantha, which is affected by this issue.

A new asset went live today that should resolve this issue, and devices should download it within the next day or so automatically. We will also address the root cause of the crash in a future version of iOS. Those of you seeing spikes in crashes in your apps should see those spikes go away shortly.
Apple did an update yesterday (Last-Modified: Tue, 26 Jan 2021 19:39:04 GMT), do you see any changes?
@ibatis Still crashing on my iPhone but it no longer crashes in the simulator. I guess it will take some time for devices to download the updated file(s).
It started working for me. Crashlytics shows 50% less crashes.
AVSpeechSynthesizer CRASH with certain voices under iOS 14.2+
 
 
Q