Fix RTCAudioDeviceModule crash during deallocation by making C++ observer teardown thread-safe#74
Merged
ipavlidakis merged 3 commits intodevelopfrom Feb 26, 2026
Conversation
…ardown races When `RTCAudioDeviceModule` is deallocated, its C++ observer/native state can be torn down while audio callbacks or thread shutdown are still in progress, which can lead to `EXC_BAD_ACCESS` during `object_cxxDestruct` in Swift ARC teardown paths. This change makes deallocation safer by: - Capturing `_native`, `_workerThread`, and `_observer` locally before teardown. - Guarding worker-thread teardown work behind `native && workerThread && !IsQuitting()`. - Detaching observer on the worker thread before deleting it. - Avoiding unsafe assumptions about worker-thread availability during object destruction. Build verification: - Ran: `bundle exec fastlane ios build verbose:true zip_product_skip:true root:/Users/ipavlidakis/workspace/webrtc/src skip_licenses:true maccatalyst_support:false` - Result: success
martinmitrevski
approved these changes
Feb 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This change addresses a crash path where AudioDeviceModule.__ivar_destroyer() could crash during app UI teardown / call end because Objective-C++ ivar destruction races with audio-engine lifecycle/worker thread callbacks.
Problem
Crash signature: EXC_BAD_ACCESS in object_cxxDestruct after AudioDeviceModule dealloc.
Triggered during call teardown, while callbacks/release chain from Swift (Call/WebRTCCoordinator) was unwinding.
Root cause
RTCAudioDeviceModule destructor currently removed observer/native linkage and deleted the C++ observer without fully validating thread/teardown state.
If worker thread is gone or quitting, callback paths and object state can become inconsistent.
Fix
In RTCAudioDeviceModule.mm (dealloc), deallocation now:
snapshots native/thread/observer pointers first,
performs detach (SetObserver(nullptr) and observer->delegate_ = nil) only when safe on a non-quitting worker thread,
only then deletes observer state.
avoids unsafe destruction assumptions when thread is unavailable.
Validation
fastlane build command above completes successfully end-to-end.
Notes
No integration-side API changes required.
If consumers perform custom ADM lifecycle mutation, prefer serializing module replacement only after full stop/shutdown flow.