1

将项目列表加载到 UITableview 并能够单击并显示所选行的警报。但是,在警报上说“确定”之后,我重新单击已经选择的行,我的代码分解为“线程 1:程序收到信号:EXC_BAD_ACCESS”。请看下面的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *playerselected = [exercises objectAtIndex:indexPath.row];
    NSString *video = [playerselected valueForKey:@"video"];

    NSString *msg = [[NSString alloc] initWithFormat:@"You have selected %@", video];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Player selected" 
                                                    message:msg 
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];    

    [alert show];     
    [alert release];
    [video release];
    [msg release]; 

}

请建议我这里可能是什么问题。

4

1 回答 1

2

不要释放video

当你从一个NSDictionary你不拥有它的值中检索一个值时,除非你明确retain它。

更具体地说,当您检索字符串时,它仍然归字典所有。当你release这样做时,你释放了一个你不拥有的对象,导致它被过度释放。结果它被释放了,当你下次尝试访问它时,内存不再有效,你的应用程序崩溃了。

于 2011-09-02T04:43:26.833 回答