简体   繁体   English

Objective-C 弱引用僵尸

[英]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.

相关问题 在Objective-C中将对象分配给弱引用? - Assigning object to weak reference in Objective-C? Objective-C ARC __weak变量引用触发警告“无法在自动变量上指定__weak属性” - Objective-C ARC __weak variable reference triggers warning “__weak attribute cannot be specified on an automatic variable” Objective-C:使用弱引用 - Objective-C: work with weak references NSString的Objective-C ARC和__weak - Objective-C ARC and __weak for NSString Objective-C ARC区块__strong __weak - Objective-C ARC block __strong __weak 在Objective-C中使用@property和弱属性 - Usage of @property and weak properties in Objective-C 当从Objective-C中的块内访问时,对象是否需要(a)`__block`修饰符或(b)弱引用? - Does object need (a) `__block` modifier or (b) weak reference, when accessed from within a block in Objective-C? 如何在 ARC 下的 Objective-C 上清零弱引用变为 nil 时得到通知? - How to get notified when a zeroing weak reference becomes nil on Objective-C under ARC? 已将Objective-C消息发送到地址为以下地址的已释放“ NSThread”对象(僵尸): - An Objective-C message was sent to a deallocated 'NSThread' object (zombie) at address: 已将Objective-C消息发送到地址为已释放的对象(僵尸) - An Objective-C message was sent to a deallocated object (zombie) at address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM