【问题标题】:NSFetchRequest release causes app to crashNSFetchRequest 发布导致应用程序崩溃
【发布时间】:2012-05-09 05:24:22
【问题描述】:

静态分析器一直告诉我,我的请求对象的保留计数为 +1,仪器告诉我那里有泄漏。但是,无论我尝试在哪里发布它,它都会让我的应用程序崩溃。 NSPredicate 对象也是如此。请帮忙,我正在努力赶上最后期限。

// Fetch Requests

// Method that returns an array of NSManagedObjects in the managedObjectContext with a predicate of who ordered

- (NSArray *)fetchDataWithEntity:(NSString *)entity andSortKey:(NSString *)key andPerson:(Person *)whoOrdered

{

 NSFetchRequest *request = [[NSFetchRequest alloc] init];

 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity 

                                                                   inManagedObjectContext:managedObjectContext];

 request.entity = entityDescription;



 // Handling Sorting

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:YES 

                                                                                 selector:@selector(compare:)];

 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

 [request setSortDescriptors:sortDescriptors];

 [sortDescriptor release];

 [sortDescriptors release];



 // Handling Predicate

 if (whoOrdered) {

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ == whoOrdered", whoOrdered];

      [request setPredicate:predicate];

 }



 NSError *error = nil;

 NSArray *mutableFetchResults = [managedObjectContext executeFetchRequest:request error:&error];

 //[request release];

 [error release];

 if (mutableFetchResults == nil) {

      // Handle the error

 }

 return mutableFetchResults;

}

【问题讨论】:

    标签: iphone memory-management crash memory-leaks nsfetchrequest


    【解决方案1】:

    没有必要释放error 对象,因为您不是它的所有者,您应该在返回mutableFetchResults 对象之前释放request 对象,因为您是使用alloc 创建它的,因此是它的主人...

    【讨论】:

    • 感谢您注意到这一点,但它仍然无法正常工作。无论我在哪里尝试发布请求,它都会崩溃。我也得到 56.73% [request setPredicate:[NSPredicate predicateWithFormat:@"...", whoOrdered]];作为我泄密的来源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-06-04
    • 2011-08-24
    相关资源
    最近更新 更多