什么是 NSZombie?

发布于 2024-10-02 03:46:57 字数 97 浏览 0 评论 0原文

我看到建议说在调试时将 NSZombieEnabled 设置为 true 。什么是 NSZombie?它是一个框架吗?一个设置?

I've seen suggestions saying to set NSZombieEnabled to true while debugging. What is NSZombie? Is it a framework? A setting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你爱我像她 2024-10-09 03:46:57

它是内存调试辅助工具。具体来说,当您设置 NSZombieEnabled 时,每当对象达到保留计数 0 时,它就会将自身转变为 NSZombie 实例,而不是被释放。每当这样的僵尸收到消息时,它就会记录一条警告,而不是崩溃或以不可预测的方式表现。因此,您可以调试微妙的过度释放/自动释放问题,而无需高级工具或大海捞针搜索。

这个名字是一个相当明显的游戏,对象通常在达到保留计数 0 时被认为是“死”的。通过这种设置,它们继续以奇怪的半衰期存在 - 既不是活的,也不是完全死的。就像真正的僵尸一样,只不过它们吃的大脑要少得多。

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.

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.

只是在用心讲痛 2024-10-09 03:46:57

Adam 很好地解释了僵尸是什么,但使用环境变量并不是查找和跟踪僵尸的最佳方法。

僵尸检测的一个更好的方法就是使用 Instruments - 从 XCode 开始,选择“Run with Instrument”并选择“Allocations”。

然后在开始后立即停止记录,按分配工具上的“i”按钮,然后打开“启用引用计数”和“启用 NSZombie 检测”。现在在仪器中再次点击“记录”,您的应用程序将启动 - 如果发送任何僵尸对象,消息记录将停止,并且记录时间线中将弹出一个对话框 - 您可以单击该对话框以查找对象所在的每个位置保留或释放。

编辑:之前的建议是针对 XCode 3 的,这里是针对 XCode 4 的补充:

在 XCode 4.2 中,有一种更简单的机制可以使用僵尸检测 - Zombie Instrument。使用“配置文件”而不是“运行”来启动应用程序,将会出现一个仪器选择器。选择“Zombie”,应用程序将开始运行 - 执行任何导致崩溃的操作,都会弹出一个对话框,显示“Zombie Messaged”。

从那里,单击对话框中的小箭头。这将获取僵尸对象创建、保留或释放的所有时间的列表。拉起侧栏,您可以转到每个条目,查看负责保留计数中每次调整的代码的堆栈跟踪。

Adam did a great job explaining what Zombies are, but using the environment variable is not the best way to find and track these.

A much better approach to zombie detection, is just to use Instruments - from XCode start with "Run with Instrument" and choose "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.

Edit: Previous advice was for XCode 3, here's an addition for 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".

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.

落在眉间の轻吻 2024-10-09 03:46:57

我同意肯德尔添加的内容,它非常有用,但我建议仍然执行环境变量,这样您就不会忘记它们已启用。与 Cocoa Dev 的(现已过期)链接类似,我放置此链接是为了不错过它:

if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) {
    NSLog(@"ZOMBIES/AFOC ARE ENABLED!!! AAAAARRRRRRGH!!! BRAINS!!!");
} 

它很好地引起了我的注意。

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:

if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) {
    NSLog(@"ZOMBIES/AFOC ARE ENABLED!!! AAAAARRRRRRGH!!! BRAINS!!!");
} 

It catches my attention very nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文