将对象设置为 nil iOS7 vs iOS8

Setting object to nil iOS7 vs iOS8

WI 有一个 iPad 信息亭应用程序,可以在通过 HDMI 连接到 iPad 的外部显示器上显示视频。我有一个 viewController 来管理外部监视器上的视图。播放完视频后,我将 MPMoviePlayerController 实例清零。在 iOS7 中,这工作正常,但在 iOS8 中,我在将 moviePlayer 设置为 nil 后遇到严重崩溃。

- (void)removeMoviePlayer {
[self.moviePlayerController.view removeFromSuperview];
[self removeMovieNotificationHandlers];
self.moviePlayerController = nil;}

启用 Zombies 后,我在调试器中收到一条消息:

[MPAVController release]: message sent to deallocated instance

同样,当应用程序在 iOS7 下运行时不会发生此崩溃。是什么改变导致了这次崩溃?

经过几天的反复试验,我发现当 MPMoviePlayerPlaybackState 为 MPMoviePlaybackStatePaused 时尝试清零 MPMoviePlayerController 实例时,应用程序会崩溃。当视频到达结尾时,MPMoviePlayerController 发送 MPMoviePlaybackDidFinish 通知,报告播放状态为 MPMoviePlaybackStatePaused。解决方法是测试播放状态,如果暂停则调用 [MPMoviePlayerController stop]。这会将 MPMoviePlaybackState 更改为 MPMoviePlaybackStateStopped,然后您可以在不发生崩溃的情况下清除实例。

此崩溃在iOS 8 之前未发生。代码如下:

-(void)moviePlayBackDidFinish:(NSNotification *)notification {
    [self stopVideo:notification];
}

- (void)stopVideo:(NSNotification *)notification {
    if (self.moviePlayerController) {
        if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePlaying || self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) {
            [self.moviePlayerController stop];
        }
        [self cleanUpVideo];
    }
}

- (void)cleanUpVideo {
    [self killProgressTimer];

    [UIView animateWithDuration:1.0f animations:^{
        self.closedCaptionLabel.alpha = 0.0f;
        self.moviePlayerController.view.alpha = 0.0f;
        self.backgroundImageView.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [self removeMoviePlayer];
        [self resetClosedCaptions];
        [self.delegate videoDidStop];
    }];
}

- (void)removeMoviePlayer {
    [self.moviePlayerController.view removeFromSuperview];
    [self removeMovieNotificationHandlers];
    self.moviePlayerController = nil;

}