NSSortDescriptor代码中的iOS内存泄漏
我在下面的类助手函数中遇到内存泄漏(请参见下面的>>)NSSortDescriptor代码中的iOS内存泄漏,ios,xcode,memory-leaks,nsarray,nsentitydescription,Ios,Xcode,Memory Leaks,Nsarray,Nsentitydescription,我在下面的类助手函数中遇到内存泄漏(请参见下面的>>) + (NSArray *)findAllRoomsInContext:(NSManagedObjectContext *)context; { NSEntityDescription *entity = [self entityDescriptionInContext:context]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorele
+ (NSArray *)findAllRoomsInContext:(NSManagedObjectContext *)context;
{
NSEntityDescription *entity = [self entityDescriptionInContext:context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entity];
>> NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
>> NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
if (error != nil)
{
//handle errors
}
sortDescriptors = nil;
return results;
}
您可以分配
sortDescriptor
和sortDescriptor
,但不能在最后释放它们
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
正在使用ARC,因此如果我发布,它将不会编译。您确定要使用ARC吗?因为在你的代码中有一个
autorelease
,所以我在编译过程中将该文件从ARC中排除。谢谢。