【问题标题】:Why does this method without fail produce a "malloc_error_break" every run?为什么这种方法每次运行都会产生“malloc_error_break”?
【发布时间】:2013-08-26 23:12:44
【问题描述】:

我有一个方法用于从 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) 失败(错误代码=12)

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

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

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

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

* 错误:无法分配区域 * 在 malloc_error_break 中设置断点进行调试 2013-08-26 19:01:15.644 PIslandTerrainGenerator[29202:a0b] beach.upper_elevation_threshold PSIslandTerrainGenerator(29202,0x1eb3a28) malloc: *** mmap(size=2097152) failed (error code=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) failed (error code=12)

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

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

【问题讨论】:

  • 您是否“在 malloc_error_break 中设置断点进行调试”?
  • @CarlNorum - 我不确定“malloc_error_break”的确切位置,所以我没有设置断点。

标签: objective-c xcode debugging runtime-error out-of-memory


【解决方案1】:

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

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

elementWithPath: 的呼叫附近。另见the documentation

【讨论】:

  • 哇!我用@autoreleasepool 包围了方法主体,内存消耗从每次运行时出现运行时错误的3 GB 下降到30 MB 以下并且没有错误。非常感谢。
猜你喜欢
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2016-03-29
  • 1970-01-01
相关资源
最近更新 更多