Skip to content

Commit

Permalink
[camera]revert the weakSelf in FLTThreadSafeEventChannel (flutter#6303)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellohuanlin authored Aug 23, 2022
1 parent 38cd9af commit e4cabdf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.8+5

* Fixes a regression introduced in 0.9.8+4 where the stream handler is not set.

## 0.9.8+4

* Fixes a crash due to sending orientation change events when the engine is torn down.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ - (void)testSetStreamHandler_shouldDispatchToMainThreadIfCalledFromBackgroundThr
[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)testEventChannel_shouldBeKeptAliveWhenDispatchingBackToMainThread {
XCTestExpectation *expectation =
[self expectationWithDescription:@"Completion should be called."];
dispatch_async(dispatch_queue_create("test", NULL), ^{
FLTThreadSafeEventChannel *channel = [[FLTThreadSafeEventChannel alloc]
initWithEventChannel:OCMClassMock([FlutterEventChannel class])];

[channel setStreamHandler:OCMOCK_ANY
completion:^{
[expectation fulfill];
}];
});

[self waitForExpectationsWithTimeout:1 handler:nil];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ - (instancetype)initWithEventChannel:(FlutterEventChannel *)channel {

- (void)setStreamHandler:(NSObject<FlutterStreamHandler> *)handler
completion:(void (^)(void))completion {
__weak typeof(self) weakSelf = self;
// WARNING: Should not use weak self, because FLTThreadSafeEventChannel is a local variable
// (retained within call stack, but not in the heap). FLTEnsureToRunOnMainQueue may trigger a
// context switch (when calling from background thread), in which case using weak self will always
// result in a nil self. Alternative to using strong self, we can also create a local strong
// variable to be captured by this block.
FLTEnsureToRunOnMainQueue(^{
typeof(self) strongSelf = weakSelf;
if (!strongSelf) return;
[strongSelf.channel setStreamHandler:handler];
[self.channel setStreamHandler:handler];
completion();
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_avfoundation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_avfoundation
description: iOS implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_avfoundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.8+4
version: 0.9.8+5

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit e4cabdf

Please sign in to comment.