【问题标题】:autorelease caused a system crash iOSautorelease导致iOS系统崩溃
【发布时间】:2013-04-01 08:20:44
【问题描述】:

我有没有ARC的方法来读取plist文件内容:

-(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)。今天是第二天,不知道用这个方法做什么。

【问题讨论】:

  • 内存泄漏但在哪里? plistPath、plistXML、格式、临时、项目...哪一个?
  • 发布items,而不是整个objectForKey:
  • @AnoopVaidya items - 这是我必须释放的唯一一个对象。但我不明白 - 自动释放有什么问题..
  • @CodaFi 对我来说现在很难理解我必须在哪里释放它。它是全局变量。我不知道如何正确释放它 - 所以我试图添加自动释放..但崩溃
  • 尝试在 dealloc 中发送release

标签: ios objective-c memory-management autorelease


【解决方案1】:

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

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

【讨论】:

  • if (items != nil) 是多余的。将-release(或任何其他方法)发送到nil 始终是安全的。
猜你喜欢
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 2014-05-07
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多