I’m using AVAudioEngine to in my host app. On very rare occasions I get a crash with error code 4099 (NSXPCConnectionInvalid), or with error code 4097 (NSXPCConnectionInterrupted) while connecting an audio unit to the main mixer node using connect(_:to:format:)
method of AVAudioEngine.
After several test, I got to the conclusion that this crash happens if the audio unit gets invalidated during this connection. I’m using kAudioComponentInstanceInvalidationNotification
to observe audio unit invalidations, but I get this notification after the crash:
Terminating app due to uncaught exception ‘com.apple.coreaudio.avfaudio’, reason: ‘error 4097’
First throw call stack:
(0x19a000f20 0x191f06018 0x19a0ff66c 0x1b3b78198 0x1b3bc3874 0x1b3bc149c 0x1b3c75208 0x1b3c78b5c 0x10261ac34 0x10264d344 0x10264e214 0x102459638 0x103d2e7bc 0x103d318e0 0x103d48cc8 0x103d47718 0x103d3ec28 0x103d3e90c 0x199fd3710 0x199fd0914 0x199fcfcd8 0x1dee801a8 0x19c60890c 0x19c6bc9d0 0x10218de84 0x1bd681e4c)
libc++abi: terminating due to uncaught exception of type NSException
Hammond B-3X ch: 1 Audio Unit invalidated
This last line is my log message when I get the invalidation notification. So checking whether or not the audio unit is invalidated before calling connect(_:to:format:)
does not help in this situation, because it looks like the audio unit gets invalidated during connect(_:to:format:)
.
Is there any way prevent this crash? Does AVAudioEngine have an internal mechanism to stop the connection if NSXPCConnection gets interruped or becomes invalid?