ios - iOS字典导致内存泄漏

标签 ios memory-leaks

为什么以下对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类集群中的一些技巧,它可能会将源错误地报告为原始字典。尝试对您的代码运行分析,它应该向您指出这些事情。

关于ios - iOS字典导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12199075/

相关文章:

java - Log4j2 自定义 Hibernate Appender 的内存泄漏

C++重载operator+和动态内存分配

ios - 解析仪表板设置

ios - 在 iOS 中使用 afnetworking 图片下载进度条不流畅

ios - iOS7中自定义UITableViewCell宽度

ios - 在 swift 上使用带有 SKLabelNode 的 runAction 造成延迟

ios - 如何在 Swift 中定义包含另一个无参数函数的函数的返回类型?

c++ - 指向变量的指针与指向一个元素数组或零元素数组的指针相同吗?

memory-leaks - 使用 gridsearchcv 的内存泄漏

c++ - IOService 无缘无故泄漏