Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5 an instance of AVPlayerItem was deallocated

When I try to play a video from my iPhone (located in the documentsDirectory), I get the following error using iOS 5 while it was working fine with iOS 4.3:

An instance 0x168da0 of class AVPlayerItem was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0x10b570> Context: 0x0, Property: 0x117ab0>

Here is the code extract:

MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[appDelegate.mediaManager loadVideo:[element valueForAttributeNamed:@"value"]]];  

        //create a NSNotificationCenter which call moviePlaybackComplete function when video playback finished
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];  

        //display the moviePlayer view
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.fullscreen = YES;  
        [moviePlayerController play];
like image 728
woody35 Avatar asked Nov 07 '11 10:11

woody35


1 Answers

EDIT: Just after posting the answer below I noticed that whenever the controller was reallocated, the errors came back as the old controller got automatically deallocated, even if I set up absolutely no notification handlers myself. Since the failures are coming from code inside the MP framework, I would say that this seems to be an OS bug.

I encountered the same issue with a storyboard-based iOS 5 project using ARC. The problem is the use of a temporary variable on the stack to refer to the movie controller - in my case I assumed an interaction with ARC, but it may be more fundamental than that. In any event, it looks as if something gets deallocated/lost too early (e.g. when a playback error occurs) and the log gets filled with the sort of output you describe.

Storing the movie controller reference in a property defined within the owning class solved this in my case; i.e:

@interface MyClass
@property ( strong, nonatomic ) MPMoviePlayerViewController * movieController;
@end

@@implementation MyClass
@synthesize movieController = _movieController;

// ...then later, this:
//
// MPMoviePlayerController *moviePlayerController = [...];
//
// ...becomes:

self.movieController = [...];

If you're using synthesised accessors for the property then whether you're using manual or automatic reference counting the generated setter method should correctly deallocate the old movie controller (if there is one) before setting up the new one.

As a footnote, if you (say) dealloc/'unreference' (set-to-nil) the property manually in a MPMoviePlayerPlaybackDidFinishNotification notification handler then you'll probably notice that the errors come back. So don't do that :-)

like image 184
Andrew Hodgkinson Avatar answered Oct 03 '22 22:10

Andrew Hodgkinson