Background
I once ran into a multithreaded crash related to
AVAudioPlayer. This article records the investigation and
the fix.
The crash stack looked like this:
1 | Crashed: com.apple.main-thread |
After searching around, I found that other developers had also encountered similar issues:
- Crash : AVAudioPlayer(AVAudioPlayerPriv) finishedPlaying
- Crash with AVAudioPlayer
- Some iOS audio crash
I also tried a few solutions suggested online, but they did not work in this case:
Analysis
From the stack, the crash was likely caused by accessing an invalid
address inside finishedPlaying:. The presence of
_objc_msgSend also strongly suggested a multithreading
issue.
AVAudioPlayer plays audio asynchronously. It starts a
separate playback thread rather than running playback directly on the
main thread. After playback finishes, it switches back to the main
thread and calls AVAudioPlayer’s
finishedPlaying: method, which then invokes the delegate
callback.
During the switch from the playback thread back to the main thread,
AVAudioPlayer itself is retained and will not be released.
However, its delegate may already have been released. In that situation,
AVAudioPlayer may still attempt to call a method on the
delegate, which can lead to a crash.
I also found a similar explanation through Google:
1 | The cause of this problem was that the AVAudioPlayer object was trying |
The AVAudioPlayer.h header declares the delegate as
weak. At first glance, this makes the crash puzzling,
especially because it appears across many iOS versions and device
models.
1 | // AVAudioPlayer.h |
Debugging
Because finishedPlaying: is not exposed as a public
method, I hooked it so that I could inspect the stack.
During debugging, I found that AVAudioPlayer has an
internal implementation class named AudioPlayerImpl, and
this internal class also has a member variable named
_delegate.
I then checked the header for AudioPlayerImpl in more
detail: AudioPlayerImpl
header
After verification, I found that these two delegate fields pointed to the same object:
1 | (lldb) po self.delegate |
This made me suspect that the _delegate member inside
the internal AudioPlayerImpl object was the real problem.
My initial guess was that the crash happened like this:
Assume that the delegate of AVAudioPlayer
is A, and the _delegate member variable inside
AudioPlayerImpl is B.
In an ideal design, when the external delegate is released, the weak
reference A should automatically become nil. At the same
time, B should also be set to nil, and the two changes
should be kept atomic. But in some cases, A and B may not stay perfectly
synchronized. A may have already become nil, while B still
points to an invalid address. If B is accessed at that moment, the app
crashes.
Fix
Based on that suspicion, I tried a simple fix:
Before executing finishedPlaying, first check whether
AVAudioPlayer’s delegate is
nil.
- If it is
nil, also set the_delegatemember ofAudioPlayerImpltonil. - Otherwise, strongly retain
AVAudioPlayer’sdelegateso that it remains alive whilefinishedPlaying:is executing.
The defensive hook looks like this:
1 | @implementation AVAudioPlayer (Extension) |
Verification
After shipping this fix, production verification showed that it did work.