0

静态分析器一直告诉我,我的请求对象的保留计数为 +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;

}

4

1 回答 1

1

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

于 2012-05-09T05:43:43.237 回答