【问题标题】:iOS memory leak in NSSortDescriptor codeNSSortDescriptor 代码中的 iOS 内存泄漏
【发布时间】:2012-08-30 03:29:57
【问题描述】:

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

【问题讨论】:

    标签: ios xcode memory-leaks nsarray nsentitydescription


    【解决方案1】:

    你分配了 sortDescriptorsortDescriptors 但最后不释放它们

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

    【讨论】:

    • 使用 ARC,所以如果我发布它不会编译。
    • 您确定您使用的是 ARC 吗?因为你的代码中有一个autorelease
    • 猜猜我在编译期间从 ARC 中排除了该文件。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多