2013-04-09 53 views
0

我有没有ARC的方法来读取plist文件内容:自动释放造成系统崩溃的iOS

-(void)readAppFile 
{ 
    NSString *plistPath = [self getDataFileDestinationPath]; 
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    NSMutableDictionary *temp = (NSMutableDictionary *) [NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
    if (!temp) { 
     NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
    } 
    items = [[temp objectForKey:@"Items"] mutableCopy]; 
    NSLog(@"Read file!"); 
} 

我在这里有一个伟大的内存泄漏!所以我用这行items = [[[temp objectForKey:@"Items"] mutableCopy] autorelease];代替代码的结尾,但现在我有Thread 1: EXC_BAD_ACCESS (code=1, addres=0x6000000008)。今天是第二天,我不知道如何处理这种方法。

+0

MEM泄漏,但在哪里? plistPath,plistXML,格式,温度,项目......哪一个? – 2013-04-09 18:36:00

+0

release'items',而不是整个'objectForKey:'东西 – CodaFi 2013-04-09 18:36:23

+0

@AnoopVaidya项目 - 这是我必须发布的唯一一个对象。但我不明白 - autorelease有什么问题.. – ShurupuS 2013-04-09 18:44:26

回答

0

尝试明确释放items重新分配它之前:

if (items != nil) [items release]; 
items = [[temp objectForKey:@"Items"] mutableCopy]; 
+2

'if(items!= nil)'是多余的。发送'-release'(或任何其他方法)到'nil'总是安全的。 – grahamparks 2013-04-09 20:29:20