【问题标题】:NSFetchedResultsController initWithFetchRequest throws EXE-BAD-ACCESS exceptionNSFetchedResultsController initWithFetchRequest 抛出 EXE-BAD-ACCESS 异常
【发布时间】:2011-02-04 14:52:47
【问题描述】:

我在UINavigationController 的子类UITableViewController 中有一个NSFetchedResultsController。当我运行该应用程序时,前三次我访问视图时一切正常(转到它,然后单击“返回”,然后再次转到它),但在第四次(总是)它崩溃并显示以下内容:

-[NSEntityDescription subentitiesByName]: message sent to deallocated instance 0x8b09c80

任何帮助将不胜感激。


这是我的结果控制器的吸气剂:

- (NSFetchedResultsController*)eventsResultsController {

  if (eventsResultsController_ == nil) {

    NSFetchRequest *aFetchRequest = [[PADataContext sharedInstance] makeGetAllFetchRequestForEntity:@"PAEvent" sortedBy:@"when" ascending:NO];

    // NOTE: crashes on this next line
    NSFetchedResultsController *aFetchedResultsContorller = [[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest managedObjectContext:[[PADataContext sharedInstance] managedObjectContext] sectionNameKeyPath:@"whenMonth" cacheName:@"AllEvent"];

    self.eventsResultsController = aFetchedResultsContorller;

    [aFetchedResultsContorller release];

  }

  return eventsResultsController_;

}

这是我用来创建NSFetchRequest的代码:

- (NSFetchRequest*)makeGetAllFetchRequestForEntity:(NSString*)entityName sortedBy:(NSString*)sortString ascending:(BOOL)ascending {

  NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
  NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
  [fetchRequest setEntity:entity];

  if (sortString != nil) {

    // add sorting information
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortString ascending:ascending];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptors release];
    [sortDescriptor release];

  }

  [entity release];
  return fetchRequest;

}

上下文对象创建一次,并在应用程序的整个生命周期中以单例形式保存。

我已检查确保在视图控制器获得dealloced 时释放eventsResultsController_

在堆栈中,我被告知它在 initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName 方法中崩溃了:

#0  0x00f04057 in ___forwarding___
#1  0x00f03f22 in __forwarding_prep_0___
#2  0x00da1b4d in -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:]
#3  0x00008c94 in -[PAHistoryListTableView eventsResultsController] at PAHistoryListTableView.m:125
#4  0x00008a5b in -[PAHistoryListTableView loadData] at PAHistoryListTableView.m:52
#5  0x00008a2a in -[PAHistoryListTableView viewDidLoad] at PAHistoryListTableView.m:43
...

【问题讨论】:

    标签: objective-c uitableview nsfetchedresultscontroller ios-4.2


    【解决方案1】:

    您使用非保留调用创建了您的实体:

    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
    

    然后你发布了它:

    [entity release];
    

    取出释放。当您专注于过度发布的 NSEntityDescription 这一事实时,这是微不足道的。你只在一个地方使用它,所以很容易找到错误。

    【讨论】:

    • NSFetchRequestsetEntity 方法不会保留它吗?
    • 不。苹果的内存管理规则在这里:developer.apple.com/library/mac/#documentation/cocoa/conceptual/…
    • 这不关你的事。那是 NSFetchRequest 的事。如果 NSFetchRequest 保留它,则 NSFetchRequest 有责任释放它。你只释放你保留的东西(通过调用allocretainnew)。
    • 我明白了,既然我没有allocNSEntityDescription,我不应该担心它。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多