0

我在以下 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;
}
4

1 回答 1

1

你分配但sortDescriptor最后sortDescriptors不释放它们

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 
于 2012-08-30T03:38:17.193 回答