【问题标题】:Getting a crash with deallocated object释放对象导致崩溃
【发布时间】:2012-01-14 23:12:23
【问题描述】:

我因以下错误消息而崩溃:2012-01-14 15:09:36.667 [8274:15203] *** -[ExerciseHistoryDetailViewController controllerWillChangeContent:]: message sent to deallocated instance 0x8d0e1f0

这里是代码。知道是什么原因造成的吗?

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.historyTableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.historyTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.historyTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.historyTableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.historyTableView endUpdates];
}

编辑,我用相同的工作流程让应用再次崩溃,但调试输出略有不同。

这次错误是:2012-01-14 17:35:48.793 [9012:15203] *** -[DetailViewController controllerWillChangeContent:]: message sent to deallocated instance 0x7fb6100

这是它崩溃的行:(![managedObjectContext save:&amp;error]) 方法是这样的:

-(IBAction)createSet
{    
    Set *set = (Set *)[NSEntityDescription insertNewObjectForEntityForName:@"Set" inManagedObjectContext:managedObjectContext];
    [self.exercise addSetsObject:set];

    NSError *error = nil;
    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"error: %@", error);
    [self checkIfEmpty];
    self.fetchedResultsController = nil; 
    [setsTableView reloadData];
}

【问题讨论】:

  • 嗯,原因是您(或系统,代表您)尝试使用已删除的对象执行controllerWillChangeContent。大概这意味着您的 NSFetchedResultsControllerDelegate 实例已被删除。可能是因为你发布了一个版本太多反对它。
  • 我正在使用 ARC,所以我没有发布任何内容...
  • @jonkroll 这个问题的答案解决了我的崩溃问题。谢谢。

标签: iphone objective-c cocoa-touch


【解决方案1】:

在这种情况下,追踪罪魁祸首的最快方法是启用 NSZombies。阅读here 怎么做。启用僵尸后,您的程序应该在导致崩溃的确切行停止。

我找到了一个关于如何在 iOS here 上调试内存问题的很好的教程。

【讨论】:

  • 我再次运行应用程序并将更新后的控制台输出放入问题中。
猜你喜欢
  • 2011-08-07
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多