开发者

NSMutableArray causing EXC_BAD_ACCESS crash after viewcontroller did disappear and re appears

Still "somewhat" of a newbie... I have an NSMutableArray that stores filenames - when a user clicks a UITableView the corresponding selected cell will pass the certain filename in the array to MPMoviePlayerController to play. It works, however if I exit out of the viewcontroller and come back, only the last video that I played will work, if I select any other table entry, I get a crash with "EXEC_BAD_ACCESS". So I'm assuming the array is being released when the view controller disappears Here is the code:

first off: "NSMutableArray *filenameArray;" is in the .h file

(void)viewDidLoad 
{
  [super viewDidLoad];
  //filenameArray = [[[NSMutableArray alloc]    initWithCapacity: 500] autorelease];
  filenameArray = [[NSMutableArray alloc] initWithCapacity: 500];    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath 
{
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) 
  {
    cell = [[[UITableViewCell alloc]    initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]    autorelease];
  }

  // Configure the cell...
  NSString *fullPath = [[NSString alloc] init];
  fullPath = [[_importableVideoDocs    objectAtIndex:indexPath.row]    description];
  NSLog(@"Full path is: %@", fullPath);
  [filenameArray addObject:fullPath];
  NSString *fileName = [fullPath lastPathComponent];
  [fullPath release];

  cell.textLabel.text = fileName;
  return cell;    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath 
{
  NSLog(@"Tapping row %d", indexPath.row);
  NSUInteger i = (indexPath.row);
  NSString *test = [[filenameArray objectAtIndex:i] description];

  //CRASH HAPPENS HERE IF VIEW CONTROLLER IS DISMISSED AND THEN    APPEARS AGAIN
  //when view disappears is filenameArray released? do I need to    retain?

  NSLog(@"%@", test);

  moviePlayer = [[[CustomMoviePlayerViewController    alloc] init] autorelease];

  // Show the movie player as modal
  [self presentModalViewController:moviePlayer    animated:YES];

  // Prep and play the movie
  [moviePlayer readyPlayer:test];
}

So my "newbie" question is, how do I retain this to stop the EXC_BAD_ACCESS crash when I click the table when the view appears for the second time? or if I'm not on the right track to the answer, what do I 开发者_如何学Cneed to do to stop this crash? If anyone could help me with how I might solve this it would be greatly appreciated! Thanks!


EXC_BAD_ACCESS usually means you're trying to use a variable that has been released. If the crash happens where you indicated its either the array or the object stored in the array. You can try adding more NSLogs before the crash, but as Nick suggested, the debugger is your friend.

If I had to guess, I'd say try the following:

NSString *fullPath = [[NSString alloc] initWithString:[[_importableVideoDocs objectAtIndex:indexPath.row] description]];

I think the way you're coding it now allocs a String, then leaks it when it's replaced by the description. The description I think would be autoreleased, so when you release it later bad stuff might happen. By using initWithString you ensure the right retain count.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜