diff --git a/media/capture/video/apple/video_capture_device_avfoundation.mm b/media/capture/video/apple/video_capture_device_avfoundation.mm index f748a6e570d68..4f1b4d354b521 100644 --- a/media/capture/video/apple/video_capture_device_avfoundation.mm +++ b/media/capture/video/apple/video_capture_device_avfoundation.mm @@ -631,7 +631,12 @@ AVCaptureDeviceFormat* FindBestCaptureFormat( // `_pendingTakePhotos` just went from 0 to 1. In case the 60 second delayed // task to perform stopPhotoOutput() is in-flight, invalidate weak ptrs to // cancel any such operation. - _weakPtrHolderForTakePhoto.weak_ptr_factory.InvalidateWeakPtrs(); + // `_lock` is required because captureOutput:didFinishProcessingPhoto:error: + // calls GetWeakPtr() on this factory from the AVFoundation delegate queue. + { + base::AutoLock lock(_lock); + _weakPtrHolderForTakePhoto.weak_ptr_factory.InvalidateWeakPtrs(); + } // Ready to take a photo immediately? // Thread-safe because `_photoOutput` is only modified on the main thread. @@ -778,10 +783,12 @@ AVCaptureDeviceFormat* FindBestCaptureFormat( if (!_photoOutput) { return; } - // Cancel all in-flight operations. - _weakPtrHolderForTakePhoto.weak_ptr_factory.InvalidateWeakPtrs(); { base::AutoLock lock(_lock); + // Cancel all in-flight operations. `_lock` must be held because + // captureOutput:didFinishProcessingPhoto:error: calls GetWeakPtr() on this + // factory from the AVFoundation delegate queue while holding `_lock`. + _weakPtrHolderForTakePhoto.weak_ptr_factory.InvalidateWeakPtrs(); if (_captureSession) { [_captureSession removeOutput:_photoOutput]; }