2010-10-08 70 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