NSSortDescriptor代码中的iOS内存泄漏

时间:2012-08-30 03:29:58

标签: ios xcode memory-leaks nsarray nsentitydescription

我在以下CLASS辅助函数中遇到内存泄漏(请参阅下面的>>)。

    + (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;
}

1 个答案:

答案 0 :(得分:1)

您分配了sortDescriptorsortDescriptors但未在末尾发布

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];