You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[video_player] Fix iOS crash with multiple players (flutter#4202)
This PR fixes crash in `video_player` package on iOS.
flutter#124937
### Detailed description
I can observe the crash when displaying a couple of the different players (different URLs) inside a `ListView`. The crash happens inside of `AVFoundation` framework:
```objc
[AVPlayer _createAndConfigureFigPlayerWithType:completionHandler:]
```
In order to debug the issue, I ran the application using the plugin with `Zombie Objects` inspection turned on. The `Zombie Objects` reports the following issue:
```
*** -[FLTVideoPlayer retainWeakReference]: message sent to deallocated instance 0x6030009b2e10
```
This, in conjunction with the `NSKeyValueWillChange` line present in the stack trace led me to believe, that culprit is sending a KVO notification to the `FLTVideoPlayer` instance that's deallocated.
Next, I examined the plugin code and identified one property that doesn't have the KVO removed. In `addObserversForItem:player:` method in `FLTVideoPlayerPlugin.m`:
```
[player addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:rateContext];
```
The observer for `@"rate"` is never cleaned up. To be entirely sure that the issue comes from KVO that's not cleaned up, I've added the following class:
```
@implementation EmptyObserver
- (void)observeValueForKeyPath:(NSString *)path
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {}
@EnD
```
and registered the observation as follows (notice that `EmptyObserver` is never retained and deallocated instantly):
```
[player addObserver:[[EmptyObserver alloc] init]
forKeyPath:@"rate"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:rateContext];
```
The exception I got seems to be matching the underlying issue:
```
*** -[EmptyObserver retainWeakReference]: message sent to deallocated instance 0x6020001ceb70
```
This means the fix for the issue is to add the following to `disposeSansEventChannel` method:
```
[self.player removeObserver:self forKeyPath:@"rate"];
```
After applying the patch, I can no longer crash the player.
0 commit comments