2013-04-09 25 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