Memory leaks when playing a video iPhone

2019-07-10 00:20发布

问题:

I need to play a simple video on my app. I been looking on the internet on how to play a video and I found out that I need to import the MediaPlayer.framwork which I did. I have a Video named:

and the code that I have in order to play it:

NSString *url = [[NSBundle mainBundle] 
                 pathForResource:@"Final_Valores_Pacific" 
                 ofType:@"m4v"];


MPMoviePlayerController *player = 
                                [[MPMoviePlayerController alloc] 
                                initWithContentURL:[NSURL fileURLWithPath:url]];

I am missing the code to add it to a view but just that code creates a leak:

what is the right way of playing a video? how can avoid that memory leak?

回答1:

Are you getting this leak on the simulator? I got this on an app that I worked on in the past only on the simulator, but not on the iPhone.

There are similar questions about this too:

iPhone: OpenAL & AudioToolbox leak



回答2:

When you alloc an object, you have to release it at the end:

// ...some code
NSString *url = [[NSBundle mainBundle] 
                 pathForResource:@"Final_Valores_Pacific" 
                 ofType:@"m4v"];


MPMoviePlayerController *player = 
                                [[MPMoviePlayerController alloc] 
                                initWithContentURL:[NSURL fileURLWithPath:url]];
// ...use player
[player release];