Search code examples
iosipadsdkautomatic-ref-countingmpmovieplayercontroller

Overreleased MPMoviePlayerController under ARC in iOS SDK 8.4 on iPad


Created a very simple (single view) sample project from a part of a bigger one. It worked with the iOS SDK 8.3.

When you hit the "Show me" button, a video comes up (as modal) and 2 secs later, it disappears. Looks good. But few moments later the app crashes because a -dealloc message is sent to an already deallocated object.

[MPAVController release]: message sent to deallocated instance

Here's my sample project (don't forget about the Video.mp4):

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

#import "ViewController.h"

#import "MediaViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Show me!" forState:UIControlStateNormal];
    button.frame = CGRectMake(10.0, 10.0, 100.0, 30.0);
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonTapped:(id)sender
{
    MediaViewController *mediaVC = [[MediaViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:mediaVC animated:YES completion:^{
        [self performSelector:@selector(dismissPresentedController) withObject:nil afterDelay:2.0];
    }];
}

- (void)dismissPresentedController
{
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}

@end

MediaViewController.h

#import <UIKit/UIKit.h>

@interface MediaViewController : UIViewController

@end

MediaViewController.m

#import "MediaViewController.h"

#import <MediaPlayer/MediaPlayer.h>

@interface MediaViewController ()

@property (nonatomic) MPMoviePlayerController *movieController;

@end

@implementation MediaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] initWithContentURL:[[NSBundle mainBundle] URLForResource:@"Video" withExtension:@"mp4"]];
    movieController.repeatMode = MPMovieRepeatModeOne;
    movieController.controlStyle = MPMovieControlStyleNone;
    movieController.scalingMode = MPMovieScalingModeAspectFill;
    [movieController prepareToPlay];
    self.movieController = movieController;

    UIView *movieView = movieController.view;
    movieView.frame = CGRectZero;
    movieView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view insertSubview:movieView atIndex:0];

    // Autolayout

    NSDictionary *layoutNeededViews = NSDictionaryOfVariableBindings(movieView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[movieView]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:layoutNeededViews]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[movieView]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:layoutNeededViews]];
}

@end

If I turn off the ARC, the bug is still alive (adding -fno-objc-arc flag for the MediaViewController.m and adding this line

[movieController release];

after the property assign.

Also checked with Instruments: Instruments check for Zombie objects

The issue occurs on iPad (all the time) but it seems not a problem on iPhone.

Has anyone faced this issue?

(Probably a solution: download the iOS 8.3 SDK and make archive with that)

Cheers,

Adam


Solution

  • Hmm.. maybe I got a fix for that, just try it (it already works for me - did many tests)

    Try this: On your MediaViewController add/override viewDidDisappear method like this:

       func viewDidDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
    
            //just a small fix for error on iPad iOS8.4
            self.movieController.stop()
        }
    

    If it don't works add it also in viewWillDisappear - worked for me.