Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
On iOS, CameraView.CaptureImage occasionally never completes. There is no MediaCaptured, no MediaCaptureFailed and no exception. With CancellationToken.None the caller waits forever. With a token, the call ends only when that token cancels.
In our app this happened in 1 of 20 captures in one session on an iPhone XR, running a Debug build where nursery collections are frequent. Earlier sessions ended with the app stuck on the camera screen.
PlatformTakePicture in CameraManager.macios.cs does not keep its photo delegate alive until the callback, which explains how this can happen:
var wrapper = new AVCapturePhotoCaptureDelegateWrapper();
photoOutput.CapturePhoto(capturePhotoSettings, wrapper);
var result = await wrapper.Task.WaitAsync(token);AVCapturePhotoOutputkeeps only a weak reference to its delegate. Apple's AVCamManual sample says so in a comment. It stores each in-progress photo capture delegate itself until the capture completes.wrapperis not used after theawait, so the optimized package build does not hoist it into the async state machine. The 6.0.1 assembly hoists no local ofPlatformTakePicture.- After
CapturePhotoreturns, nothing managed references the wrapper. The wrapper references itsTaskCompletionSource, but nothing references the wrapper, and the only native reference is AVFoundation's weak one. - A GC between
CapturePhotoandDidFinishProcessingPhotocan therefore collect the managed peer and release the native delegate. AVFoundation then has nobody to call, and the task never completes.
The same kind of flaw was fixed for video recording (#3152): videoRecordingDelegate is now a field. On main, the photo delegate is still a local.
Our logs cannot tie a GC to that exact capture, so treat this as the most likely explanation the source supports, not a proven one.
Expected Behavior
Expected Behavior
CaptureImage always completes, either with the photo or by raising MediaCaptureFailed.
Steps To Reproduce
Steps To Reproduce
The failure depends on timing, so it is intermittent. It needs a GC in the window between CapturePhoto and the callback.
- Show a
CameraViewon an iOS device and callCaptureImagerepeatedly. It is more likely on an older device and in a Debug build, where collections are frequent. - Occasionally a call never returns.
Forcing a full collection right after starting the capture should make it much more likely. We have not verified this as a deterministic repro:
var capture = cameraView.CaptureImage(CancellationToken.None);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
await capture; // may never completeLink to public reproduction project repository
None yet. The defect is in the source quoted above, and the failure depends on timing.
Environment
- CommunityToolkit.Maui.Camera 6.0.1 (on
mainthe photo delegate is still a local) - .NET 10, .NET MAUI 10.0.51
- iPhone XR, iOS 18.7.10
Link to public reproduction project repository
Needs reproduction
Environment
- CommunityToolkit.Maui.Camera 6.0.1 (on `main` the photo delegate is still a local)
- .NET 10, .NET MAUI 10.0.51
- iPhone XR, iOS 18.7.10Anything else?
Anything else?
Suggested fix: keep the delegate reachable from the CameraManager until its callback, as was done for videoRecordingDelegate. Use a field, or a dictionary keyed by capturePhotoSettings.UniqueID if overlapping captures should be allowed, and clear it once the await completes.
A GC.KeepAlive(wrapper) after the await is not enough. It would hoist wrapper into the state machine, but with CancellationToken.None the state machine is itself only reachable through the wrapper's own task, so the whole cycle stays collectable.
Workaround: pass a token with a timeout and call CaptureImage again when it cancels. A fresh call creates a fresh delegate.
This workaround is not safe on Android with 6.0.1. There, one retained ImageCallBack serves every call and PlatformTakePicture ignores the token, so a late first photo would complete the second call.
Activity
dotnet-policy-service commentedon Sep 24, 2026
Hi @@elfiservice! We have added the needs reproduction label to this issue, which indicates that we are unable to take further action until you provide a reproduction sample that we can use to reproduce + fix your bug. In accordance with the
Reporting a bugsection of ourContributing.mdthat you confirmed reading before you submitted this Issue, we ask that every Issue author provide a link to an open-source repository containing the reproduction sample app. This issue will be closed automatically in 5 days if we do not receive a reproduction sample from you. Please feel free to re-open this Issue once you have provided a reproduction sample.dotnet-policy-service commentedon Sep 28, 2026
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 3 days. It will be closed if no further activity occurs within 2 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
dotnet-policy-service commentedon Oct 1, 2026
We haven't received a reproduction sample from you. The issue is closed.