objective-c 如何找到malloc“double free”错误的原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971249/
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
How to find the cause of a malloc "double free" error?
提问by gonso
I'm programming an application in Objective-C and I'm getting this error:
我正在使用 Objective-C 编写应用程序,但出现此错误:
MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free
*** set a breakpoint in malloc_error_break to debug
MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free
*** 在 malloc_error_break 中设置断点进行调试
It is happening when I release an NSAutoreleasePool and I can't figure out what object I'm releasing twice.
当我释放一个 NSAutoreleasePool 并且我无法弄清楚我释放了两次什么对象时就会发生这种情况。
How do I set his breakpoint?
我如何设置他的断点?
Is there a way to know what is this "object 0x1068310"?
有没有办法知道这个“对象0x1068310”是什么?
采纳答案by Frank Krueger
You'll find out what the object is when you break in the debugger. Just look up the call stack and you will find where you free it. That will tell you which object it is.
当您中断调试器时,您会发现对象是什么。只需查看调用堆栈,您就会找到释放它的位置。这将告诉您它是哪个对象。
The easiest way to set the breakpoint is to:
设置断点的最简单方法是:
- Go to Run -> Show -> Breakpoints (ALT-Command-B)
- Scroll to the bottom of the list and add the symbol
malloc_error_break
- 转到运行 -> 显示 -> 断点(ALT- Command- B)
- 滚动到列表底部并添加符号
malloc_error_break
回答by Quinn Taylor
When an object is "double-freed", the most common cause is that you're (unnecessarily) releasing an autoreleased object, and it is later autoreleased when the containing autorelease pool is emptied.
当一个对象被“双重释放”时,最常见的原因是你(不必要地)释放了一个自动释放的对象,当包含的自动释放池被清空时它会被自动释放。
I've found that the best way to track down the extra release is to use the NSZombieEnabledenvironment variable for the affected executable in Xcode. For a quick rundown of how to use it, check out this CocoaDev wiki page. (In addition to this page, Apple has documented some incredibly obscure yet useful tips for debugging code in Xcode, some of which have saved my bacon more than a few times. I suggest checking out this Technical Noteon developer.apple.com — link jumps to the section on Cocoa's Foundation framework).
我发现追踪额外版本的最佳方法是在 Xcode 中为受影响的可执行文件使用NSZombieEnabled环境变量。要快速了解如何使用它,请查看此 CocoaDev wiki 页面。(除了这个页面,Apple 还记录了一些在 Xcode 中调试代码的非常晦涩但有用的技巧,其中一些已经多次救了我的培根。我建议在 developer.apple.com 上查看这个技术说明- 链接跳转到 Cocoa 的基础框架部分)。
Edit:You can often track the offending object down within the Xcode debugger, but it's often much easier if you use Instruments to assist you. From Xcode, choose Run → Start With Performance Tool → Object Allocationsand you should be able to trace the offending object back to where it was created. (This will work best if you're enabled zombies as discussed above.) Note:Snow Leopard adds a Zombies tool to Instruments, accessible from the Run menu as well. Might be worth the $29 alone! ;-)
编辑:您通常可以在 Xcode 调试器中跟踪有问题的对象,但如果您使用 Instruments 来帮助您,通常会容易得多。在 Xcode 中,选择Run → Start With Performance Tool → Object Allocations,您应该能够将有问题的对象追溯到它的创建位置。(如果您如上所述启用了僵尸,这将最有效。)注意:Snow Leopard 向仪器添加了一个僵尸工具,也可以从运行菜单访问。可能值 29 美元!;-)
There is also a related SO question here.
这里还有一个相关的 SO 问题。
回答by Hoang Pham
I just want to add my experience in addition to the answer of Quinn Taylor.
除了 Quinn Taylor 的回答之外,我只想补充一下我的经验。
In one of my apps, I have to parse and save data into core data objects and later on get these objects to display on the views. In fact, the app works just fine and does not crash at all, until I tried to do a stress test of navigating back and forth multiple times, tried to open multiple views as fast as possible. The app crashes with the above message.
在我的一个应用程序中,我必须解析数据并将其保存到核心数据对象中,然后让这些对象显示在视图上。事实上,该应用程序运行良好,根本不会崩溃,直到我尝试进行多次来回导航的压力测试,尝试尽可能快地打开多个视图。应用程序崩溃并显示上述消息。
I have tried all the methods that Quinn suggested in his answer and still failed to find out where was the exact cause.
我已经尝试了 Quinn 在他的回答中建议的所有方法,但仍然无法找出确切原因。
I set NSZombieEnabled=YES, and NSStackLogging=YES, ran the command shell malloc_history to find out why, but still no luck. It always points out to where I save the data into core data objects, in fact, I have checked thousand times the over released objects there, nothing odd.
我设置了 NSZombieEnabled=YES 和 NSStackLogging=YES,运行命令 shell malloc_history 找出原因,但仍然没有运气。它总是指出我将数据保存到核心数据对象的位置,事实上,我在那里检查了数千次过度释放的对象,没什么奇怪的。
Running in Instruments with various tools(Allocations, Leaks, etc...) still did not help. Enable the Guard Malloc still got nothing.
使用各种工具(分配、泄漏等)在仪器中运行仍然没有帮助。启用Guard Malloc还是一无所获。
Final rescue: I tried to come back to the views where the objects were taken from Core Data and sent a retain message to all of these objects, and took note to these changes. It solved the issue!!!
最后的救援:我试图回到从 Core Data 中获取对象的视图,并向所有这些对象发送保留消息,并注意到这些更改。它解决了问题!!!
So, I found out that I failed to retain one, that's exactly the cause. Just want to share my experience so you have another rescue for your app.
所以,我发现我没有保留一个,这正是原因。只是想分享我的经验,以便您的应用程序获得另一个拯救。
回答by Adam Rosenfield
Open up the debugger console by pressing Cmd+Shift+R. There, type
按 Cmd+Shift+R 打开调试器控制台。在那里,输入
break malloc_error_break
to set a breakpoint at the beginning of the malloc_error_breakfunction.
在malloc_error_break函数的开头设置断点。
If you want to find out what object is located at address 0x1068310, you can type the following into the debugger console:
如果要找出地址 0x1068310 处的对象,可以在调试器控制台中键入以下内容:
print-object 0x1068310
Of course, you have to do this while the object is still alive -- if the object has already been freed by the time you do this, then this will not work.
当然,你必须在对象还活着的时候这样做——如果在你这样做时对象已经被释放,那么这将不起作用。
回答by c-alpha
For me the issue was solved by
对我来说,问题已解决
(gdb) call (void)_CFAutoreleasePoolPrintPools()
right after the crash. The address at the top of the stack was the address of the culprit. Threw in a retainand voila.
坠机之后。堆栈顶部的地址是罪魁祸首的地址。扔了进去retain,瞧。
The address given in the log message did not get me anywhere. It never showed up in any of the various Instrumets. Apparently a pointer to some internal data which had already been freed.
日志消息中给出的地址没有让我到任何地方。它从未出现在各种乐器中。显然是指向一些已经被释放的内部数据的指针。
回答by Old McStopher
Adding a symbolic breakpoint in Xcode 4
在 Xcode 4 中添加符号断点
Just an update to make this relevant to Xcode 4...
只是一个更新,使其与 Xcode 4 相关...
From the Xcode 4 User Guide:
来自Xcode 4 用户指南:
To add a symbolic breakpoint . . .
- In the bottom-left corner of the breakpoint navigator, click the Add button.
- Choose Add Symbolic Breakpoint.
- Enter the symbol name in the Symbol field.
- Click Done.
添加符号断点。. .
- 在断点导航器的左下角,单击添加按钮。
- 选择添加符号断点。
- 在符号字段中输入符号名称。
- 单击完成。
回答by Martijn Thé
This is what the malloc_error_break breakpoint looks like in the Breakpoints window in Xcode. Need to check the boxes to make it work.
这就是 Xcode 的 Breakpoints 窗口中 malloc_error_break 断点的样子。需要选中这些框才能使其工作。
alt text http://www.martijnthe.nl/wp-content/uploads/2009/08/Afbeelding-1.png
替代文字 http://www.martijnthe.nl/wp-content/uploads/2009/08/Afbeelding-1.png
回答by Wes Duff
Check your classes and look under the dealloc method. Make sure you care calling [super dealloc].
检查您的类并查看 dealloc 方法。确保你在乎打电话[super dealloc].
I had this exact same problem and found out I was calling [self dealloc]instead. Just not paying attention.
我遇到了完全相同的问题,并发现我正在打电话[self dealloc]。就是没注意。
回答by Ramkrishna Sharma
Please find the below steps for how to find the object which is free and crash the application.
请找到以下步骤,了解如何找到免费的对象并使应用程序崩溃。
1) Click on the "Breakpoint navigator".
2) Then click on the "+" button which is below.
3) Add the "Symbolic Breakpoint..." from the list.
4) Add the "malloc_error_break" keyword on the "Symbol" option.
1) 单击“断点导航器”。
2) 然后点击下方的“ +”按钮。
3)从列表中添加“符号断点...”。
4)在“ Symbol”选项上添加“ malloc_error_break”关键字。
Or you can also refer the below GIF presentation.
或者您也可以参考下面的 GIF 演示文稿。
回答by Kris Roofe
This is usually caused by some inspector, such as safari or safari preview. Refer to postor postand question.
这通常是由某些检查器引起的,例如 safari 或 safari preview。请参阅帖子或帖子和问题。
Remove the select of AutoMatically Show Web ...., will remove this issue.
删除 AutoMatically Show Web .... 的选择,将删除此问题。
Note, just close safari or safari preview will not remove this issue. And you have to deselect both of safari and safari preview.
请注意,仅关闭 safari 或 safari preview 不会消除此问题。并且您必须取消选择 safari 和 safari preview。
If this will not do, refer to this answeror postto debug it.


