[BUG] [iOS] CameraView: AVCaptureVideoPreviewLayer deallocated while key value observers were still registered

Aperta
#3,356 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp, ios
Ambito
mobile-dev

Direzione di ricerca

Start by reading the iOS CameraManager implementation, especially Dispose() and PlatformStopCameraPreview, and trace how rotationCoordinator observes previewView.Layer. Verify that coordinator cleanup occurs before preview-view disposal and remains safe for stopped previews. A public reproduction is unavailable, so success requires a regression test or reproducible lifecycle scenario showing no exception after transient page navigation and finalization.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 17+, the app aborts with an NSInternalInconsistencyException some seconds after leaving a page that hosts a CameraView. The page is transient (not reused) and the CameraView handler is never disconnected explicitly, so the page and its native views are released later by the GC.

NSInternalInconsistencyException: An instance of class AVCaptureVideoPreviewLayer was deallocated while key value observers were still registered with it.
Current observation info:
  Observer: 0x0, Key path: bounds, Options: <New: YES, Old: YES, Prior: NO>
  Observer: 0x0, Key path: frame,  Options: <New: YES, Old: YES, Prior: NO>

Native stack of the crashed (main) thread:

+[__NSObject_Disposer drain:]
Foundation.NSObject.ReleaseManagedRef
xamarin_release_managed_ref
NSKVODeallocate
objc_exception_throw

The release comes from the finalizer path (NSObject_Disposer), not from an explicit Dispose.

Analysis (from reading CameraManager for macios in 6.1.0):

  • On iOS 17+, PlatformConnectCamera creates an AVCaptureDeviceRotationCoordinator bound to the preview view's layer: rotationCoordinator = new AVCaptureDeviceRotationCoordinator(captureDevice, previewView.Layer).
  • The only observers registered on the preview layer appear to be the coordinator's (bounds / frame). This is our interpretation; we have not confirmed it in Apple's code.
  • CameraManager.Dispose() disposes previewView before rotationCoordinator. When the handler is not disconnected, both are finalized in arbitrary order, so the layer can be deallocated while the coordinator still observes it.
Expected Behavior

Leaving a page with a CameraView (with or without an explicit handler disconnect) never aborts the app. The preview layer is never deallocated while an observer is still registered on it.

Suggested fix:

  • In CameraManager.Dispose(), dispose the rotation coordinator before the preview view, and set it to null.
  • Consider releasing the coordinator in PlatformStopCameraPreview as well, so a stopped preview leaves no observer bound to the layer.
Steps To Reproduce
  1. Register a page with a CameraView as transient and navigate to it (Shell push); start the preview.
  2. Stop the preview and navigate back. The page is not reused and the handler is not disconnected.
  3. Keep using the app until the GC collects the old page. The app aborts with the exception above.

It is intermittent because it depends on GC timing and finalization order (one occurrence in our field reports so far).

Link to public reproduction project repository

No public reproduction yet: the crash depends on GC timing and has been seen once in the field. The analysis above is from the shipped 6.1.0 assembly. Happy to build a minimal repro if it helps.

Environment
- .NET MAUI CommunityToolkit: CommunityToolkit.Maui.Camera 6.1.0
- OS: iOS 27.0.1 (iPhone18,2); the rotation coordinator path is iOS 17+
- .NET MAUI: 10.0.110
Anything else?

The crash happens on the main thread during the disposer drain, with no managed frame of the app involved.

Lingua principale
C#
Stelle
2.7k
Fork
502
Merge medio
11h 10m
PR unite (30g)
50

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di CommunityToolkit/Maui

Tutte le issue di CommunityToolkit/Maui

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.