从数据库检索数据时内存泄漏

时间:2011-03-21 13:25:05

标签: iphone objective-c memory-leaks nsfetchrequest

嘿,我已经为数据库访问创建了一个自定义检索方法:

+(NSArray*) recordsForTable:(NSString *)table predicate:(NSPredicate *)prd{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:table inManagedObjectContext:managedObjectContext];

    [fetchRequest setEntity:entity];

    [fetchRequest setPredicate:prd];

    NSArray *records = [managedObjectContext executeFetchRequest:fetchRequest error:nil];

    [fetchRequest release];

    return records;
}
然后我在这个方法中使用上面的方法:

-(NSArray *)tableViewControllerData{

    NSNumber *savedBool = [[NSNumber alloc] initWithBool:YES];

    NSString *onlyGetSavedVisitObjects = [NSString stringWithFormat:@"bolSaved=%@", savedBool];
    [savedBool release];
    NSMutableArray *data = [[[CoreDataAccess recordsForTable:@"LPVisit" stringPredicate:onlyGetSavedVisitObjects] mutableCopy] autorelease];
    NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dteVisitDate" ascending:NO];
    NSArray *descriptors = [NSArray arrayWithObjects:dateDescriptor, nil];
    [data sortedArrayUsingDescriptors:descriptors];

    return data;    
}

我遇到的麻烦是,当用户对LPVisit表进行更改并调用此方法来显示这些更改时,它会使应用程序崩溃。

[编辑]

它产生的例外是:

 -[__NSArrayM objectID]: unrecognized selector sent to instance 0x4dac1f0

我认为错误在线:

NSMutableArray *data = [[[CoreDataAccess recordsForTable:@"LPVisit" stringPredicate:onlyGetSavedVisitObjects] mutableCopy] autorelease];

如果我删除自动释放,我会收到内存泄漏,但应用程序不会崩溃。

有人有任何见解,提前谢谢

1 个答案:

答案 0 :(得分:0)

可变复制是否可能导致异常,因为*recordsnil?如果@"bolSaved=%@"是拼写错误且可能是@"boolSaved=%@",则可能会发生这种情况。