2010-10-08 69 views
0

仍然“有点”新手...我有一个NSMutableArray存储文件名 - 当用户单击UITableView时,相应的选定单元格将传递数组中的某个文件名到MPMoviePlayerController播放。它可以工作,但是如果我退出视图控制器并返回,只有我播放的最后一个视频才会起作用,如果我选择任何其他表项,我会碰到“EXEC_BAD_ACCESS”。所以我假设阵列在视图控制器消失时被释放 这里是代码:NSMutableArray导致EXC_BAD_ACCESS在viewcontroller消失后出现崩溃并重新出现

first off:“NSMutableArray * filenameArray;”在.h文件

(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]; 
} 

所以我的“新手”问题是,我该如何保持这种停止EXC_BAD_ACCESS崩溃当我点击表格视图时出现第二次?或者如果我的回答不正确,我需要做些什么来阻止这次事故?如果有人可以帮我解决这个问题,我将非常感谢! 谢谢!

+0

您通常会受益于从断点处逐步完成代码。 Stacktrace也应该帮助你隔离这个。你怎么知道崩溃在这里与正在卸载的视图有关? – Nick 2010-10-08 06:09:41

回答

2

EXC_BAD_ACCESS通常表示您正尝试使用已发布的变量。如果崩溃发生在您指定阵列或存储在阵列中的对象的位置。您可以尝试在崩溃前添加更多的NSLog,但正如Nick所建议的,调试器是您的朋友。

如果我猜的话,我会说尝试以下方法:

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

我想你现在编码的方式allocs一个字符串,那么它泄漏时,它被描述替代。我认为这个描述是自动发布的,所以当你发布它时,可能会发生不好的事情。通过使用initWithString,您可以确保正确的保留计数。

+0

是的!描述是自动发布的,并将代码更改为您在上面发布的代码片段。非常感谢 – NSDestr0yer 2010-10-08 14:01:00

+0

从一个“有点”的newb到另一个,我很乐意帮助...甚至更快乐,它的工作! – coastwise 2010-10-08 23:53:50

+0

我正要将我的头发拉出来,然后找到答案。谢谢! – MCR 2013-01-31 16:21:41