Skip to content

[BUG] iOS: CameraView.CaptureImage can hang forever — the photo capture delegate is not kept alive until DidFinishProcessingPhoto #3322

Closed

Description

@elfiservice

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

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);
  • AVCapturePhotoOutput keeps 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.
  • wrapper is not used after the await, so the optimized package build does not hoist it into the async state machine. The 6.0.1 assembly hoists no local of PlatformTakePicture.
  • After CapturePhoto returns, nothing managed references the wrapper. The wrapper references its TaskCompletionSource, but nothing references the wrapper, and the only native reference is AVFoundation's weak one.
  • A GC between CapturePhoto and DidFinishProcessingPhoto can 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.

  1. Show a CameraView on an iOS device and call CaptureImage repeatedly. It is more likely on an older device and in a Debug build, where collections are frequent.
  2. 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 complete

Link 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 main the 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.10

Anything 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

  1. dotnet-policy-service commented on Sep 24, 2026

    @dotnet-policy-service
    Contributor

    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 bug section of our Contributing.md that 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.

  2. dotnet-policy-service commented on Sep 28, 2026

    @dotnet-policy-service
    Contributor

    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.

  3. dotnet-policy-service commented on Oct 1, 2026

    @dotnet-policy-service
    Contributor

    We haven't received a reproduction sample from you. The issue is closed.

  4. locked and limited conversation to collaborators on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions