更改moviePlayer contentURL时的EXC_BAD_ACCESS

时间:2010-12-22 17:45:01

标签: objective-c memory-leaks release mpmovieplayercontroller exc-bad-access

简而言之,我的应用程序正在这样做:

1)我的主视图(RootViewController)有一个按钮,当我点击它时,它显示播放器(PlayerViewController):

2)在我的播放器中,我初始化了我想播放的视频

- >它运作良好,我的电影是显示

我的问题:

当我回到主视图时:

然后我再次点击按钮,我收到*程序接收信号:“EXC_BAD_ACCESS”。*

如果我发表评论self.player.contentURL = [self movieURL];它正在发挥作用,但当我放弃它时,我有这个问题。

我读到这是由于空指针或内存问题,但我不明白为什么它第一次工作而不是第二次工作。我用dealloc方法释放了我的对象。

感谢您的帮助!

布鲁诺。

这是我的代码:

根视图控制器

RootViewController.h

#import <UIKit/UIKit.h>
#import "PlayerViewController.h"


@interface RootViewController : UIViewController {
    IBOutlet UIButton * myButton;
}

@property (nonatomic,retain) IBOutlet UIButton * myButton;

-(IBAction)displayPlayer:(id)sender;
- (void) returnToRoot: (PlayerViewController *) controller;

@end

RootViewController.m

#import "RootViewController.h"


@implementation RootViewController

@synthesize myButton;


-(IBAction)displayPlayer:(id)sender
{
    PlayerViewController *playerViewController = [[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil];

    playerViewController.delegate = self;

    playerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentModalViewController: playerViewController animated: YES];
    [playerViewController release];
}

- (void) returnToRoot: (PlayerViewController *) controller
{
    [self dismissModalViewControllerAnimated: YES];
}


- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
    [super dealloc];
}


@end

播放器视图控制器

PlayerViewController.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMoviePlayerController.h>

@protocol PlayerViewControllerDelegate;

@interface PlayerViewController : UIViewController {
    UIView *viewForMovie;
    MPMoviePlayerController *player;
}
@property (nonatomic, assign) id <PlayerViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIView *viewForMovie;
@property (nonatomic, retain) MPMoviePlayerController *player;

- (NSURL *)movieURL;
-(IBAction)goBackToRoot:(id)sender;

@end

@protocol PlayerViewControllerDelegate
- (void) returnToRoot: (PlayerViewController *) controller;
@end

PlayerViewController.m

#import "PlayerViewController.h"


@implementation PlayerViewController

@synthesize player;
@synthesize viewForMovie;
@synthesize delegate;


- (void)dealloc {
    [super dealloc];
    [player release];
    [viewForMovie release];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad");
    self.player = [[MPMoviePlayerController alloc] init];
    [self.player autorelease];
    self.player.view.frame = self.viewForMovie.bounds;
    self.player.view.autoresizingMask = 
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight;
    [self.viewForMovie addSubview:player.view];
    self.player.contentURL = [self movieURL];
    [self.player play];
}

-(NSURL *)movieURL
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = 
    [bundle 
     pathForResource:@"myVideo" 
     ofType:@"mp4"];
    if (moviePath) {
        return [NSURL fileURLWithPath:moviePath];
    } else {
        return nil;
    }
}

-(IBAction)goBackToRoot:(id)sender{
    [self.delegate returnToRoot: self];
}


- (void)viewDidUnload {
    [super viewDidUnload];
}

@end

问题

第二次调用“displayPlayer”时,我有EXC_BAD_ACCESS

2 个答案:

答案 0 :(得分:3)

我解决了!!!

我查看MPMoviePlayerController以查看哪个变量是contentURL

(NSURL *)contentURL

这意味着我也要解放它。

我在dealloc方法中使用nil值:

-(void) dealloc {
   [super dealloc];
   self.player.contentURL = nil;
   [player release];
   [viewForMovie release];
}

答案 1 :(得分:0)

  

如果我评论self.player.contentURL =   [self movieURL];它工作,但是   当我放手时,我有这个问题。

在这种情况下,如何宣布contentURL@property定义是否包含copyretain