0

我有一种方法用于从 plist 中检索数据,如下所示:

在此处输入图像描述

方法本身是这样的:

//'path' is of the form "beach.color.green" (for example)
-(NSString *)elementWithPath: (NSString *)path {

    //'self.target' is the path to the plist
    NSDictionary *currentData = [NSDictionary dictionaryWithContentsOfFile:self.target];

    //This is the number of nested layers that the desired data lies in.
    NSUInteger numberOfLayers = [[path componentsSeparatedByString:@"."] count];

    for(NSUInteger i = 0; i < numberOfLayers; i++) {

        NSString *pathComponentInCurrentLayer = [path componentsSeparatedByString:@"."][i];

        //If the data we're currently searching through is a dictionary...
        if([[currentData objectForKey:pathComponentInCurrentLayer] isKindOfClass:[NSDictionary class]])

            //Narrow our search by making that dictionary the dictionary to search through
            currentData = [currentData objectForKey:pathComponentInCurrentLayer];

        //It's not a dictionary, so it has to be the desired data...
        else

            //So we return it
            return [currentData objectForKey:pathComponentInCurrentLayer];
    }

    return nil;
}

我知道该方法工作正常,因为我通过断点逐步执行它并验证它返回了正确的输出。问题是,每次我运行我的项目(其中多次调用此方法)时,都会出现以下错误:

PSIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以调试 PIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以调试 PIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以调试 PIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以调试 PIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点进行调试 2013-08-26 19:01:15.644 PSIslandTerrainGenerator[29202:a0b] beach.upper_elevation_threshold PSIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size= 2097152) 失败(错误代码=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以进行调试 2013-08-26 19:01:15.647 PSIslandTerrainGenerator[29202:a0b] low_grass.upper_elevation_threshold PSIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size= 2097152) 失败(错误代码=12)

* 错误:无法分配区域 *在 malloc_error_break 中设置断点以进行调试

我进入 Instruments 并分析了该应用程序,它显示每当它崩溃时,该应用程序都会使用超过 3 GB 的内存。所以我猜测这个方法中的某些东西每次运行时都无法释放,但无法准确指出是什么。

4

1 回答 1

3

您正在创建大量自动释放的对象,而不会耗尽自动释放池。尝试使用

@autoreleasepool {
    // Code that creates autoreleased objects.
}

周围的某个地方的电话elementWithPath:。另请参阅文档

于 2013-08-27T00:01:02.177 回答