繁体   English   中英

iOS字典导致内存泄漏

[英]iOS Dictionary causes memory leak

为什么以下对Dictionary的实现会导致内存泄漏? 也请参见下面的屏幕截图。 实际上,所有泄漏都来自此方法。

- (void) setLocation:(NSString *) location:(NSString *) turnPage {
    NSLog(@"Start setLocation");
    //---get the path to the property list file---
    NSString *localPlistFileNameConf = [[self documentsPath] stringByAppendingPathComponent:@"Config.plist"];
    NSMutableDictionary *copyOfDict;
    //---if the property list file can be found---
    if ([[NSFileManager defaultManager] fileExistsAtPath:localPlistFileNameConf]) {
        //---load the content of the property list file into a NSDictionary object---
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:localPlistFileNameConf];
        //---make a mutable copy of the dictionary object---
        copyOfDict = [dict mutableCopy];
        [dict release];
    }
    else {
        //---load the property list from the Resources folder---
        NSString *pListPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
        //---make a mutable copy of the dictionary object---
        copyOfDict = [dict mutableCopy];
        [dict release];
    }
    location = [self checkLocationValidity:location:turnPage];
    [copyOfDict setValue:location forKey:@"Location"];
    [self writeConfigToFile:copyOfDict];
    NSLog(@"End setLocation");
}

在此处输入图片说明

您不会在任何地方释放copyOfDict 您拥有使用以copy开头的方法创建的任何对象,因此您需要释放这些对象。 由于效率原因,由于NSDictionary集中的一些NSDictionary它可能会将源误报为原始字典。 尝试对您的代码进行分析,它应该向您指出这些事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM