fix(android): prevent crash when starting broadcast from preview

Detach preview callbacks through the retained surface holder before deferred native view destruction. Cover native preview teardown followed by camera capture and document the regression check.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-05 11:54:03 +00:00
commit b98f82663d
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
5 changed files with 99 additions and 11 deletions

View file

@ -32,7 +32,7 @@ internal sealed class AndroidCapturePreview : Grid, IDisposable
private sealed class PreviewHost(AndroidCameraFrameRotator rotator, Action<Exception> failed)
: NativeControlHost, IDisposable
{
private SurfaceView? _view;
private ISurfaceHolder? _holder;
private SurfaceCallback? _callback;
private bool _disposed;
@ -40,10 +40,15 @@ internal sealed class AndroidCapturePreview : Grid, IDisposable
{
var activity = MainActivity.Current
?? throw new InvalidOperationException("Activite Android indisponible pour l'apercu.");
_view = new SurfaceView(activity);
_callback = new SurfaceCallback(this);
_view.Holder!.AddCallback(_callback);
return new AndroidViewControlHandle(_view);
var view = new SurfaceView(activity);
if (!_disposed)
{
_holder = view.Holder
?? throw new InvalidOperationException("Surface Android indisponible pour l'apercu.");
_callback = new SurfaceCallback(this);
_holder.AddCallback(_callback);
}
return new AndroidViewControlHandle(view);
}
private void SetSurface(ISurfaceHolder holder, int width, int height)
@ -60,23 +65,30 @@ internal sealed class AndroidCapturePreview : Grid, IDisposable
}
protected override void DestroyNativeControlCore(IPlatformHandle control)
{
ReleaseSurface();
base.DestroyNativeControlCore(control);
}
private void ReleaseSurface()
{
Detach();
if (_callback is not null)
{
_view?.Holder?.RemoveCallback(_callback);
// The native view wrapper may already be disposed during deferred host destruction.
_holder?.RemoveCallback(_callback);
_callback.Dispose();
_callback = null;
}
_view = null;
base.DestroyNativeControlCore(control);
_holder?.Dispose();
_holder = null;
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
Detach();
ReleaseSurface();
}
private sealed class SurfaceCallback(PreviewHost owner) : Java.Lang.Object, ISurfaceHolderCallback