ios 什么是 NSZombie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4168327/
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
What is NSZombie?
提问by Moshe
I've seen suggestions saying to set NSZombieEnabled
to true
while debugging. What is NSZombie? Is it a framework? A setting?
我看到建议说在调试时设置NSZombieEnabled
为true
。什么是 NSZombie?它是一个框架吗?一个设定?
回答by Adam Wright
It's a memory debugging aid. Specifically, when you set NSZombieEnabled
then whenever an object reaches retain count 0, rather than being deallocated it morphs itself into an NSZombie
instance. Whenever such a zombie receives a message, it logs a warning rather than crashing or behaving in an unpredictable way. As such, you can debug subtle over-release/autorelease problems without advanced tools or painstaking needle in haystack searches.
这是一个内存调试辅助工具。具体来说,当您设置NSZombieEnabled
时,只要对象达到保留计数 0,而不是被释放,它就会将自身变形为一个NSZombie
实例。每当这样的僵尸收到消息时,它都会记录警告而不是崩溃或以不可预测的方式行事。因此,您可以调试细微的过度释放/自动释放问题,而无需高级工具或在大海捞针中进行艰苦的搜索。
The name is a fairly obvious play on the fact that objects are normally considered "dead" when they reach retain count 0. With this setting, they continue to exist in a strange half-life - neither living, nor quite dead. Much like real zombies, except they eat rather fewer brains.
这个名字是一个相当明显的事实,当对象达到保留计数 0 时,它们通常被认为是“死的”。在这个设置下,它们继续存在于一个奇怪的半衰期——既不是活的,也不是完全死的。很像真正的僵尸,除了它们吃的大脑更少。
回答by Kendall Helmstetter Gelner
Adam did a great job explaining what Zombies are, but using the environment variable is not the best way to find and track these.
Adam 在解释什么是 Zombies 方面做得很好,但使用环境变量并不是查找和跟踪这些僵尸的最佳方法。
A much better approach to zombie detection, is just to use Instruments - from XCode start with "Run with Instrument" and choose "Allocations".
一种更好的僵尸检测方法,就是使用 Instruments - 从 XCode 开始,从“Run with Instrument”开始,然后选择“Allocations”。
Then stop the recording right after it starts, press the "i" button on the Allocations instrument, and turn on "enable reference counts" and "Enable NSZombie Detection". Now hit Record again in the instrument, and your app will start up - if any zombie objects are sent messages recording will stop, and a dialog box will pop up in the recording timeline - you can click on that to find every place an object was retained or released.
然后在开始后立即停止记录,按Allocations仪器上的“i”按钮,并打开“启用引用计数”和“启用NSZombie检测”。现在再次点击乐器中的录制,您的应用程序将启动 - 如果发送任何僵尸对象,则消息录制将停止,并且录制时间轴中将弹出一个对话框 - 您可以单击它以找到对象所在的每个位置保留或释放。
Edit: Previous advice was for XCode 3, here's an addition for XCode 4:
编辑:以前的建议是针对 XCode 3 的,这里是 XCode 4 的补充:
In XCode 4.2, there's an even easier mechanism to make use of Zombie detection - the Zombie Instrument. Instead of "Run" to start the app, use "Profile" and an instrument selector will come up. Select "Zombie", and the app will start running - do whatever causes your crash, an a dialog will pop up saying "Zombie Messaged".
在 XCode 4.2 中,有一个更简单的机制来使用僵尸检测——僵尸工具。使用“配置文件”而不是“运行”来启动应用程序,然后会出现一个仪器选择器。选择“Zombie”,应用程序将开始运行 - 执行任何导致崩溃的操作,会弹出一个对话框,显示“Zombie Messaged”。
From there, click the small arrow in the dialog box. That will take to a list of all the times that zombie object was created, retained, or released. Pull up the side bar and you can go to each entry, looking at the stack trace for the code that was responsible for each adjustment in the retain count.
从那里,单击对话框中的小箭头。这将列出僵尸对象被创建、保留或释放的所有时间。拉起侧栏,您可以转到每个条目,查看负责保留计数中每次调整的代码的堆栈跟踪。
回答by Matthew Frederick
I agree with what Kendall added, it's very useful, but I'll suggest still doing the environment variable so you don't forget they're enabled. Similar to the (now expired) link at Cocoa Dev, I put this so I don't miss it:
我同意 Kendall 添加的内容,它非常有用,但我建议您仍然使用环境变量,这样您就不会忘记它们已启用。与 Cocoa Dev 上的(现已过期)链接类似,我把这个链接放在这里,以免错过:
if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) {
NSLog(@"ZOMBIES/AFOC ARE ENABLED!!! AAAAARRRRRRGH!!! BRAINS!!!");
}
It catches my attention very nicely.
它很好地引起了我的注意。
回答by selva
Would help someone.
会帮助某人。
Detailed document on Instruments. https://developer.apple.com/library/watchos/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/index.html#//apple_ref/doc/uid/TP40004652-CH3-SW1