【问题标题】:Why does my application crash with the following error?为什么我的应用程序崩溃并出现以下错误?
【发布时间】:2011-05-22 18:15:23
【问题描述】:

当我上下滚动表格视图时,大约 6 到 8 次后,我的应用程序崩溃了,我在调试窗口中得到以下信息:

myapp[250:207] *** -[NSIndexPath row]: message sent to deallocated instance 0xdd0eab0

这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [remoteRecipientItems count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"RemoteRecipientItem";

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




    NSUInteger row = [indexPath row];

    NSUInteger oldRow = [lastIndexPath row];

    // Configure the cell...

    [[cell textLabel]setText:[remoteRecipientItems objectAtIndex:[indexPath row]]];

    cell.accessoryType = (row == oldRow && lastIndexPath !=nil)? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell;
}





#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



    int newRow = [indexPath row];
    int oldRow = (lastIndexPath !=nil)?[lastIndexPath row]:-1;

    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];

        oldCell.accessoryType = UITableViewCellAccessoryNone;

        lastIndexPath = indexPath;


    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;


    remoteRecipientItems = nil;
    remoteRecipientID = nil;
    xmlData = nil;
    lastIndexPath = nil;

}


- (void)dealloc {

    [remoteRecipientItems release];
    [remoteRecipientID release];
    [xmlData release];

    [lastIndexPath release];

    [super dealloc];
}

【问题讨论】:

    标签: objective-c cocoa-touch ios memory-management


    【解决方案1】:

    您不拥有 indexPath 变量,因此您需要保留它。

    尝试替换这个:

    lastIndexPath = indexPath;
    

    有了这个:

    lastIndexPath = [indexPath retain];
    

    【讨论】:

    • 所以这意味着我必须释放它?我会在哪里发布它?
    • 不是每次点击表格视图都会发生保留吗?在 dealloc 中我只会释放一次?这不会弄乱保留/释放计数吗?
    • 我想你可以像你一样在dealloc 发布它。您也可以尝试在保留indexPath 之前立即释放它。只需确保检查 lastIndexPath 是否为 nil,如果未释放,则保留新的。
    • 哎呀。 @edc1591 是对的。您应该在下一次保留之前立即释放它。至于nil,发给nil的消息不算数。
    • 创建 lastIndexPath 作为属性/同步它,当您为其分配新值时,运行时将自动为您处理所有保留/释放工作。然后你可以在 dealloc 中释放该属性。
    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多