Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Adding check for null before creating capture session. #2965

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.9

* Fix rare nullptr exception on Android.

## 0.5.8+6

* Avoiding uses or overrides a deprecated API in CameraPlugin.java.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ public void resumeVideoRecording(@NonNull final Result result) {
}

public void startPreview() throws CameraAccessException {
createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());
if (pictureImageReader != null) {
createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());
}
Comment on lines +424 to +426
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could invert the if statement to reduce nesting and would make it a bit easier to expand the method:

Suggested change
if (pictureImageReader != null) {
createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());
}
if (pictureImageReader == null) {
return;
}
createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());

Copy link
Contributor

@mvanbeusekom mvanbeusekom Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking further into this issue, it would be good to know why pictureImageReader is null at this point and if that is valid (as @tvolkert also mentioned in his comments on PR #2871 ).

If the pictureImageReader should not be null when startPreview is called, this code will hide a mistake that is somewhere else in the code. I will start looking into this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Maurits for looking into this! I agree that this is only a workaround for a symptom, but not a proper fix. Do you have a hunch where the real problem could lie?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @panmari, we (me and a colleague) have been looking into this issue and can't quite reproduce it as of yet. We do have 2 theories at the moment:

  1. Since version 0.5.0 developers are responsible for handling application lifecycle events that might affect the plugin (see also this comment mentioned by @kevin-lot). If these events are not handled correctly it might cause the issue where the permission system moves the app to the background and not reinitialize the camera plugin correctly (leaving it in an invalid state);
  2. We could did get the same error when (using the example app) very rapidly switching between the front and back facing cameras. We noticed that in these cases the onOpened callback would be called when the dispose method has already been run (race condition).

I am currently discussing on how we should approach these problems.

}

public void startPreviewWithImageStream(EventChannel imageStreamChannel)
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera
description: A Flutter plugin for getting information about and controlling the
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
and streaming image buffers to dart.
version: 0.5.8+6
version: 0.5.9

homepage: https://github.com/flutter/plugins/tree/master/packages/camera

Expand Down