NSFetchedResultsController initWithFetchRequest抛出EXE-BAD-ACCESS异常

时间:2011-02-04 14:52:48

标签: objective-c uitableview nsfetchedresultscontroller ios-4.2

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

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

非常感谢任何帮助。


这是结果控制器的getter:

- (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;

}

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

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

在堆栈中,我被告知它在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
...

1 个答案:

答案 0 :(得分:3)

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

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

然后你发布了它:

[entity release];

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