ios Malloc 错误“无法分配区域”失败,错误代码为 12。知道如何解决这个问题吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8477236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:51:03  来源:igfitidea点击:

Malloc error "can't allocate region" failed with error code 12. Any idea how to resolve this?

iosmemory-managementmallocallocation

提问by brush51

i am getting this error and dont know what to do with that:

我收到此错误,不知道该怎么办:

AppName(3786,0xa0810540) malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

If i set a breakpoint to that line that occurs the error, i dont know what i have to search specially for. In instruments i have checked the allocations and the value is increasing until 14,5 GB of all allocations.

如果我在发生错误的那一行设置了一个断点,我不知道我必须专门搜索什么。在工具中,我检查了分配,并且该值正在增加,直到所有分配的 14,5 GB。

Can someone give me help?
brush51

有人可以帮我吗?
刷51

EDIT 1:
More informations:
- I am trying this in the simulator, not on the iOS device.
- Thats all of the output(i am getting this error more times). - the error occurs on this line:

编辑 1:
更多信息:
- 我在模拟器中尝试这个,而不是在 iOS 设备上。
- 这就是所有输出(我多次收到此错误)。- 错误发生在这一行:

NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchREntitySetsCards = [[[NSFetchRequest alloc] init] autorelease];
//NSFetchRequest *fetchREntityRelCardsAnswersNotes = [[[NSFetchRequest alloc] init] autorelease];

NSEntityDescription *entitySetsCards = [NSEntityDescription entityForName:@"EntitySetsCards" inManagedObjectContext:context];
//NSEntityDescription *entityRelCardsAnswersNotes = [NSEntityDescription entityForName:@"EntityRelCardsAnswersNotes" inManagedObjectContext:context];
setEntity:entityCard];
[fetchREntitySetsCards setEntity:entitySetsCards];
//[fetchREntityRelCardsAnswersNotes setEntity:entityRelCardsAnswersNotes];

NSArray *fetchedObjSetsCards    = [context executeFetchRequest:fetchREntitySetsCards error:&error];
//The error is here--->
//NSArray *fetchedObjRelCardsAnswersNotes   = [context executeFetchRequest:fetchREntityRelCardsAnswersNotes error:&error];


//Badges für TabBarItem Inbox setzen
setsCount = [context countForFetchRequest:fetchREntityUserSet error: &error];
cardsCount = [context countForFetchRequest:fetchREntityCard error: &error];

采纳答案by eric

Googling will reveal quite a few tutorials on using instruments to understand what is going on with your memory:

谷歌搜索会发现很多关于使用仪器来了解你的记忆发生了什么的教程:

How to debug memory leaks:(tutorial)
http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial

如何调试内存泄漏:教程
http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial

And another:
Finding Obj-C memory leaks(video)
http://www.youtube.com/watch?v=R449qEuexNs&feature=related

另一个:
查找 Obj-C 内存泄漏视频
http://www.youtube.com/watch?v=R449qEuexNs&feature=related

*There are many similar questionson stackoverflow you might benefit from.

*您可能会受益于有关 stackoverflow 的许多类似问题

回答by Tamara

Sounds strange, but I had the same behavior when main thread was overloaded.

听起来很奇怪,但是当主线程过载时我有相同的行为。

Memory usage was optimal enough: instruments shows no leaks and live memory was about 2Mb, no memory warnings while running on a device, all massive allocation was done inside autorelease pools etc.

内存使用已经足够优化:仪器显示没有泄漏,实时内存约为 2Mb,在设备上运行时没有内存警告,所有大量分配都在自动释放池内完成等。

But there was very huge process of storing data to db (using Core Data) made on main thread. Just moving the storing code to background process like this

但是在主线程上将数据存储到数据库(使用核心数据)的过程非常庞大。只需像这样将存储代码移动到后台进程

dispatch_async(dispatch_get_global_queue
  (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  BOOL result = NO;
  result = [[DataManager sharedInstance] storeGuestsToDB];
  dispatch_async(dispatch_get_main_queue(), ^{
  //finalization
  }
}

fixed my problem.

解决了我的问题。

回答by JAL

I had this issue due to a recursive call in a view controller's viewWillLayoutSubviews. I was invalidating the layout of a collection view causing an endless cycle of repeatedly laying out views.

由于在视图控制器的viewWillLayoutSubviews. 我使集合视图的布局无效,导致重复布局视图的无休止循环。