-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[camera] Fix CameraController may update after dispose and crash with "used after being disposed. (fixes #184959) #11498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
e7d7b65
88032f2
1d40ee8
96661b3
9dc0722
e7f6120
1805506
367e0e9
ff89a34
e3b32f5
a9a2cd0
fd3f963
5a7fcdc
446fd5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,7 +341,9 @@ class CameraController extends ValueNotifier<CameraValue> { | |
| _deviceOrientationSubscription ??= CameraPlatform.instance | ||
| .onDeviceOrientationChanged() | ||
| .listen((DeviceOrientationChangedEvent event) { | ||
| value = value.copyWith(deviceOrientation: event.orientation); | ||
| if (!_isDisposed) { | ||
| value = value.copyWith(deviceOrientation: event.orientation); | ||
| } | ||
| }); | ||
|
|
||
| _cameraId = await CameraPlatform.instance.createCameraWithSettings( | ||
|
|
@@ -361,7 +363,9 @@ class CameraController extends ValueNotifier<CameraValue> { | |
| CameraPlatform.instance.onCameraError(_cameraId).first.then(( | ||
| CameraErrorEvent event, | ||
| ) { | ||
| value = value.copyWith(errorDescription: event.description); | ||
| if (!_isDisposed) { | ||
| value = value.copyWith(errorDescription: event.description); | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
|
|
@@ -370,26 +374,28 @@ class CameraController extends ValueNotifier<CameraValue> { | |
| imageFormatGroup: imageFormatGroup ?? ImageFormatGroup.unknown, | ||
| ); | ||
|
|
||
| value = value.copyWith( | ||
| isInitialized: true, | ||
| description: description, | ||
| previewSize: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => | ||
| Size(event.previewWidth, event.previewHeight), | ||
| ), | ||
| exposureMode: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.exposureMode, | ||
| ), | ||
| focusMode: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.focusMode, | ||
| ), | ||
| exposurePointSupported: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.exposurePointSupported, | ||
| ), | ||
| focusPointSupported: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.focusPointSupported, | ||
| ), | ||
| ); | ||
| if (!_isDisposed) { | ||
| value = value.copyWith( | ||
| isInitialized: true, | ||
| description: description, | ||
| previewSize: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => | ||
| Size(event.previewWidth, event.previewHeight), | ||
| ), | ||
| exposureMode: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.exposureMode, | ||
| ), | ||
| focusMode: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.focusMode, | ||
| ), | ||
| exposurePointSupported: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.exposurePointSupported, | ||
| ), | ||
| focusPointSupported: await initializeCompleter.future.then( | ||
| (CameraInitializedEvent event) => event.focusPointSupported, | ||
| ), | ||
| ); | ||
| } | ||
|
Comment on lines
+377
to
+398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The final CameraInitializedEvent event = await initializeCompleter.future;
if (!_isDisposed) {
value = value.copyWith(
isInitialized: true,
description: description,
previewSize: Size(event.previewWidth, event.previewHeight),
exposureMode: event.exposureMode,
focusMode: event.focusMode,
exposurePointSupported: event.exposurePointSupported,
focusPointSupported: event.focusPointSupported,
);
} |
||
| } on PlatformException catch (e) { | ||
| throw CameraException(e.code, e.message); | ||
| } finally { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could these use early returns? That would reduce the indent changes in code.
Just a personal preference though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! Early returns would definitely improve the code.
However, this PR is still in draft. @mbcorona from the Flutter team recently
discovered a deeper issue: ImageReader fails to drain pending frames on dispose,
flooding the queue with 290+ orphaned callbacks.
flutter/flutter#184959 (comment)
My current fix addresses the symptom (guarding state updates). but the root cause is the improper
ImageReadercleanup at the nativelevel. So I'm currently reinvestigating the issue
Once I resolve the frame leakage issue, I'll:
Thanks again for your suggestion!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before marking it ready for review, please update the PR to use and follow our actual checklist, rather than an AI-hallucinated checklist.