NSFetchedResultsController initWithFetchRequest引发EXE-BAD-ACCESS异常 [英] NSFetchedResultsController initWithFetchRequest throws EXE-BAD-ACCESS exception

查看:90
本文介绍了NSFetchedResultsController initWithFetchRequest引发EXE-BAD-ACCESS异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UINavigationControllerUITableViewController的子类中有一个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;

}

上下文对象仅创建一次,并在应用程序的整个生命周期中保持为单一状态.

我已经检查了确保在视图控制器被dealloc编辑时释放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
...

解决方案

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

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

然后您将其释放:

[entity release];

取出发行版.当您关注NSEntityDescription被过度发布的事实时,这很容易找到.您只在一个地方使用它,因此很容易发现错误.

I have an NSFetchedResultsController inside a subclass of UITableViewController inside a UINavigationController. When I run the app, everything works perfectly the first three times I access the view (going to it, then clicking 'Back', then going to it again), but on the fourth (always) it crashes with the following:

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

Any help would be much appreciated.


Here is my getter for the results controller:

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

}

This is the code I use to create my 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;

}

The context object is created once and held throughout the lifetime of the app in a singleton.

I have checked the ensure the eventsResultsController_ gets released when the view controller gets dealloced.

In the stack, I'm told it crashed in the initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName method:

#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
...

解决方案

You created your entity with a non-retaining call:

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

And then you released it:

[entity release];

Take out the release. This is trivial to find when you focus on the fact that it was the NSEntityDescription that was over-released. You only use that in one place, so it's easy to find the mistake.

这篇关于NSFetchedResultsController initWithFetchRequest引发EXE-BAD-ACCESS异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆