[英]Objective-C weak reference zombie
I'm trying to create a zombie object to detect sending messages to a deallocated object.我正在尝试创建一个僵尸对象来检测向已解除分配的对象发送消息。
Say i have a strong property object A with a weak reference to object B. When B is deallocated my weak reference becomes nil but calling a method eg [obj1.obj2 somemethod] simply returns nil not causing a crash.假设我有一个强属性对象 A 对对象 B 的弱引用。当 B 被释放时,我的弱引用变为 nil,但调用一个方法,例如 [obj1.obj2 somemethod] 只是返回 nil 而不会导致崩溃。
Is there a way to test zombies using weak references?有没有办法使用弱引用来测试僵尸? I can only crash using unsafe_unretained.我只能使用 unsafe_unretained 崩溃。
Is there a way to test zombies using weak references有没有办法使用弱引用来测试僵尸
(I take it that by "zombies" you actually mean dangling pointers...) (我认为“僵尸”实际上是指悬空指针......)
Not directly, no.不直接,不。 The whole point of ARC-weak references is that they prevent dangling pointers. ARC 弱引用的全部意义在于它们可以防止悬空指针。 (As you rightly say, they safely replace the potential dangling pointer with nil — and there's no penalty for sending a message to nil.) (正如你所说的,他们用 nil 安全地替换了潜在的悬空指针——并且向 nil 发送消息没有任何惩罚。)
The reason there can be dangling pointer crashes in real life is that most of Cocoa does not use ARC.在现实生活中可以有悬摆指针的原因崩溃的是,大多数可可的不使用ARC。 (As you rightly say, it uses unsafe_unretained
.) (正如你所说,它使用unsafe_unretained
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.