在运行时检测僵尸
有没有办法在 Objective-C 中运行时找到僵尸?
我正在寻找一种方法来防止对象调用僵尸上的方法,有没有什么方法可以检测到僵尸而不会使应用程序崩溃?
我确实了解ARC iOS5下的弱引用和常识性编程实践。
我在想有一种方法可以询问对象大小(我知道也许“内部”只有引用),但如果对象仍然存在,它应该给出一个值,如果它可能不仅仅是单个指针大小。
使用malloc_size(pointerToObject)
能行吗?
更新: 我知道如何运行僵尸检测工具
Is there a way to find a Zombie at runtime in objective-c?
I'm looking for a way to prevent an object to call a method on a zombie, is there any way to detect one without making the app crash?
I do know about weak reference under ARC iOS5 and common sense programming practice.
I was thinking that a way could be asking the object size (I know that maybe "inside" there are just reference) but if the object still exist it should give a value, if it doesn't probably just the single pointer size.
Using malloc_size(pointerToObject)
Could it work?
UPDATE:
I do know how to run Instruments for Zombies detection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为 Andrea 是在问如何运行仪器来检测僵尸,我认为她想防止在运行时调用已释放的实例。我不确定在这种情况下会返回多少 malloc 大小。我认为你想出的任何东西,除了他们对 NSZombies 所做的事情(即永远不会真正免费的实例)之外,都将是花哨的,并且只能在部分时间起作用。我认为最好的方法是使用枯燥的旧式勤奋编程和分析工具来防止进行这些调用,而不是尝试在运行时捕获错误。
I don't think Andrea is asking how to run instruments to detect zombies, I think she wants to guard against calling a dealloced instance at runtime. I'm not sure what malloc size will return in this case. I think anything you come up with short of what they've done with NSZombies (which is to never truly free instances) will be gimicky and only work part of the time. I think your best best is the boring old diligent programming and profiling with instruments to guard against making these calls rather than trying to catch the error at runtime.
尝试使用 Instruments 运行应用程序,然后选择“Zombies”模板。
Try running the application with Instruments, and select the "Zombies" template.
在方案菜单上(左上角,运行/停止旁边)。选择“编辑方案...”。将出现一张纸。在那里选择运行/调试方案。选择中心窗格上的“诊断”选项卡。检查僵尸。
On the scheme menu (upper left, next to run/stop). Select "Edit Scheme...". A sheet will appear. Select the Run/Debug scheme there. Select the "Diagnostics" tab on the center pane. Check Zombies.