Bruno
Bruno

Reputation: 73

EXC_BAD_ACCESS when I change moviePlayer contentURL

In few words, my application is doing that :

1) My main view (RootViewController) has a buton when I tap on it, it displays the player (PlayerViewController) :

2) In my Player, I initialize the video I want to play

-> It's working good, my movie is display

My problem :

When I go back to my main view :

And I tap again on the button, I get a *Program received signal: “EXC_BAD_ACCESS”.*

If I comment self.player.contentURL = [self movieURL]; it's working, but when I let it, iI have this problem.

I read that it's due to null pointer or memory problem but I don't understand why it's working the first time and not the second time. I release my object in dealloc method.

Thanks for your help !

Bruno.

Here is my code :

Root View Controller

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

Player View Controller

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

Problem

The second time I call "displayPlayer" I had the EXC_BAD_ACCESS

Upvotes: 2

Views: 1359

Answers (2)

Bruno
Bruno

Reputation: 73

I solved it !!!

I look on the MPMoviePlayerController to see what kind of variable is contentURL

(NSURL *)contentURL

It means I have also to liberate it.

I do that in my dealloc method putting a nil value:

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

Upvotes: 3

David Gelhar
David Gelhar

Reputation: 27900

If I comment self.player.contentURL = [self movieURL]; it's working, but when I let it, iI have this problem.

In that case, how is contentURL declared? Does the @property definition include copy or retain?

Upvotes: 0

Related Questions