ios 如何在 Xcode4 中调试“发送到释放实例的消息”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8298946/
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 debug "message sent to deallocated instance" in Xcode4?
提问by dontWatchMyProfile
I pressed ALT+CMD+R and activated NSZombieEnabled in Arguments > Environment Variables. Additionally, I activated it in Diagnostics > Memory Management > Enable Zombie Objects.
我按下 ALT+CMD+R 并在参数 > 环境变量中激活 NSZombieEnabled。此外,我在诊断 > 内存管理 > 启用僵尸对象中激活了它。
However, when I built and run, at some point my app crashes giving me this useless message in the console:
但是,当我构建并运行时,在某些时候我的应用程序崩溃了,在控制台中给了我这个无用的消息:
*** -[CALayer retainCount]: message sent to deallocated instance 0x656b260
The stack trace is just as useless. I moved the details-level slider all the way to the right. Thread 1 simply shows me this:
堆栈跟踪同样无用。我将细节级别滑块一直向右移动。线程 1 只是向我展示了这一点:
Everything is system-owned and there's not a single line related to my app. So obviously NSZombiesEnabled doesn't work as it did in Xcode 3, where it halted on the dead object.
一切都归系统所有,没有一行与我的应用程序相关。所以很明显 NSZombiesEnabled 不像在 Xcode 3 中那样工作,它在死对象上停止。
Is there any way to figure out whichCALayer is deallocated too early?
有什么方法可以找出过早释放哪个CALayer?
Update: So after building and running about 100 more times suddenly the problem DISAPPEARED! It's completely gone! And the best part: I did not modify my code in any way! Inbetween I cleaned the build folder and project with the clean commands several times and deleted the app in the Simulator several times as well.
更新:所以在构建和运行大约 100 次之后,问题突然消失了!完全没了!最好的部分是:我没有以任何方式修改我的代码!在此期间,我多次使用 clean 命令清理了构建文件夹和项目,并多次删除了模拟器中的应用程序。
Update 2: Fortunately the problem re-appeared. And now it seems persistent. Fortunately, because I prefer finding the root cause rather than annoying the users by random.
更新 2:幸运的是,问题再次出现。现在它似乎很持久。幸运的是,因为我更喜欢找到根本原因而不是随意惹恼用户。
Update 3: Finally found it by accident:
更新3:终于偶然发现了:
startButton = newBttn;
should have been:
本来应该:
self.startButton = newBttn;
startButton was a retaining property and in -dealloc I released it. So it got overreleased and in most (but not all) cases after the view faded out it crashed giving that weird CALayer retainCount message.
startButton 是一个保留属性,我在 -dealloc 中释放了它。因此它被过度释放,并且在大多数(但不是全部)情况下,在视图淡出后,它会崩溃并给出奇怪的 CALayer 保留计数消息。
The Zombies Instrument (CMD + I) finally pointed out that it had to do with a button. Just didn't know why and where.
Zombies Instrument (CMD + I) 最后指出它与按钮有关。只是不知道为什么和在哪里。
Clang Static Analyzer didn't complain about this obvious glitch.
Clang Static Analyzer 并没有抱怨这个明显的故障。
回答by Jeff Kelley
If this crops up again, you can run a dedicated Zombies instrument. Hit Command+I to profile the app and select the Zombies instrument (you must be running on the simulator). If you get a zombie, you can display the entire memory history (each retain/release) for that object, which is immensely helpful in tracking down errors.
如果再次出现这种情况,您可以运行专用的 Zombies 仪器。按 Command+I 配置应用程序并选择 Zombies 工具(您必须在模拟器上运行)。如果遇到僵尸,则可以显示该对象的整个内存历史记录(每个保留/释放),这对于跟踪错误非常有帮助。
回答by chown
In addition to Jeff's great answer; to do almost the same thing, but without having to open Instruments or profile your App, you can set NSZombieEnabled, MallocStackLogging, and guard mallocin the debugger. Then, when your App crashes, type this in the gdb console:
除了杰夫的精彩回答;要做几乎相同的事情,但无需打开 Instruments 或分析您的应用程序,您可以在调试器中设置NSZombieEnabled、MallocStackLogging和保护 malloc。然后,当您的应用程序崩溃时,在 gdb 控制台中输入:
(gdb) info malloc-history 0x543216
Replace 0x543216
with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.
替换0x543216
为导致崩溃的对象的地址,您将获得更有用的堆栈跟踪,它应该可以帮助您查明代码中导致问题的确切行。