iphone - Dealloc'd 谓词导致 iPhone 应用程序崩溃!

标签 iphone core-data nspredicate

作为前言,这是几天前进行的调查的后续行动: https://stackoverflow.com/questions/2981803/iphone-app-crashes-when-merging-managed-object-contexts

简短版本:EXC_BAD_ACCESS 正在使我的应用程序崩溃,僵尸模式揭示了罪魁祸首是嵌入在我的获取结果 Controller 中嵌入的获取请求中的谓词。如果没有明确的命令,如何释放对象中的对象?

长版: 应用结构 平台 View Controller -> 游戏 View Controller (根据平台选择) -> 添加游戏 View Controller

当在平台 View 上单击一行时,它会在游戏 View 中为该平台设置一个实例变量,然后游戏获取结果 Controller 以正常方式构建获取请求:

- (NSFetchedResultsController *)fetchedResultsController{
if (fetchedResultsController != nil) {
  return fetchedResultsController;
}
//build the fetch request for Games
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
  entityForName:@"Game"
  inManagedObjectContext:context];
[request setEntity:entity];
//predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"platform == %@",
  selectedPlatform];
[request setPredicate:predicate];
//sort based on name
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
  ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

//fetch and build fetched results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
  initWithFetchRequest:request 
  managedObjectContext:context 
  sectionNameKeyPath:nil 
  cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[sortDescriptor release];
[sortDescriptors release];
[predicate release];
[request release];
[aFetchedResultsController release];

return fetchedResultsController;
} 

在此方法结束时,fetchedResultsController 的 _fetch_request -> _predicate 成员被设置为 NSComparisonPredicate 对象。世界上一切都好。

当 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 被调用时,_predicate 现在是一个 Zombie,当表尝试更新自身时,它最终将使应用程序崩溃。

我或多或少感到困惑。我不会释放获取的结果 Controller 或其任何部分,唯一被释放的部分是谓词。有任何想法吗?

编辑: 作为测试,我将此行添加到 Fetched Results Controller 方法中:

[fetchedResultsController.fetchRequest.predicate retain];

现在它没有崩溃,但这看起来像是一个补丁,而不是我应该做的事情。

最佳答案

您不应该释放您的谓词变量。您没有调用 newallocretaincopy(这是“narc”规则)创建谓词,因此您不负责释放它。这就是你的僵尸的来源。

关于iphone - Dealloc'd 谓词导致 iPhone 应用程序崩溃!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994616/

相关文章:

iphone - NS未知异常: this class is not key value coding-compliant for the key piewTextField

iphone - 取消模态视图和应用程序崩溃

iphone - UIWebView 字体在纵向比横向更细

iphone - NSPredicate,带引号/不带引号

sqlite - CoreData 通过抽象托管对象获取请求到具体托管对象

iphone - 以与 Facebook oauth key 类似的方式获取 Twitter oauth key

ios - 强制设置核心数据检查点?

ios - 在 CoreData 中创建实例

iphone - iOS 中的 Core Data Spotlight 集成

ios - NSPredicate 使用实体的关系获取其他实体