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

Conversation

@hellohuanlin
Copy link
Contributor

@hellohuanlin hellohuanlin commented Aug 16, 2022

This is to fix a crash when reopening the app. More detailed discussion can be found here where I explained why orientationChange is identified as the root cause.

Also went through all other usages of "strong self", but I don't think it's worth writing unit tests for them, since writing weak self is a pretty standard practice. But still, I want to write the test for CameraPlugin::orientationChanged since it is believed to be the cause.

List which issues are fixed by this PR. You must list at least one issue.

Fixes flutter/flutter#109143

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@hellohuanlin hellohuanlin force-pushed the camera_weak_self branch 3 times, most recently from f830933 to 3925680 Compare August 16, 2022 00:37
[self.camera setDeviceOrientation:orientation];
[weakSelf.camera setDeviceOrientation:orientation];
// `CameraPlugin::sendDeviceOrientation` can be called on any queue.
[self sendDeviceOrientation:orientation];
Copy link
Contributor Author

@hellohuanlin hellohuanlin Aug 16, 2022

Choose a reason for hiding this comment

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

This line is the root cause, but i also went through all other usage of "strong self"s in case of similar crashes in the future

More detailed discussion can be found here where I explained why orientation change is identified as the root cause.

@hellohuanlin hellohuanlin marked this pull request as ready for review August 16, 2022 00:42
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

Perhaps this could also use:

- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
  [UIDevice.currentDevice endGeneratingDeviceOrientationNotifications];

And additionally in -detachFromEngineForRegistrar something to clear out the camera inProgressSavePhotoDelegates to release their completion blocks? Unfortunately we can't also do queue cancellation as with NSOperationQueues (which I'll never fail to bring up whenever we discuss the gcd API 🙂 )

_inProgressSavePhotoDelegates = [NSMutableDictionary dictionary];

__weak typeof(self) weakSelf = self;
FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) {
if (error) {
[result sendFlutterError:error];
Copy link
Member

Choose a reason for hiding this comment

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

If self is nil this shouldn't be sending error messages or it will hit the same crash. It also shouldn't continue on to schedule FLTRequestAudioPermissionWithCompletionHandlers. I recommend the strongSelf/return early pattern for all of the non-trivial blocks.

[[FLTThreadSafeMethodChannel alloc] initWithMethodChannel:methodChannel];
}

- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
Copy link
Contributor Author

@hellohuanlin hellohuanlin Aug 16, 2022

Choose a reason for hiding this comment

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

I did not clear out inProgressSavePhotoDelegates since the delegate does not hold strong reference to its owner anymore (deleted the outdated comment here)

@hellohuanlin hellohuanlin requested a review from jmagman August 16, 2022 21:01
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

LGTM with nits and one question

@@ -1,3 +1,7 @@
## 0.9.8+4

* Fixes a crash due to sending orientation change events when the engine is teared down.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Fixes a crash due to sending orientation change events when the engine is teared down.
* Fixes a crash due to sending orientation change events when the engine is torn down.

FLTEnsureToRunOnMainQueue(^{
[self.channel setStreamHandler:handler];
[weakSelf.channel setStreamHandler:handler];
completion();
Copy link
Member

Choose a reason for hiding this comment

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

Should the completion be run if this is dead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me being lazy. Should've used if (!strongSelf) return; here.

Looking at the current usage, running completion should be fine, but I think it's better not to run it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS] Crash while reopening app

2 participants