如果我们启用僵尸对象,所有弱引用都会被清零吗? [英] Are all weak references nilled if we enable zombie object?

查看:38
本文介绍了如果我们启用僵尸对象,所有弱引用都会被清零吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果一个对象被释放,所有弱引用都将被清除.

如果我们启用僵尸对象呢?

产品 -> 编辑方案 -> 启用僵尸对象

弱引用也会被清零吗?

否则正常情况下不会出现的bug会出现在enable僵尸对象上

我强烈怀疑,正如其中一个答案所说,弱引用将被清除.

话虽如此,

如何修复当 UIViewController 已经消失时调用 viewDidScroll 的错误? 并在那里回答建议.

解决方案

即使启用了 Zombie Objects,弱引用也会被设置为 nil.

我是这样测试的:

id __weak o;@autoreleasepool {id ob = [[NSObject alloc] init];o = ob;NSLog(@"发布前弱引用 %@", o);}NSLog(@"发布后弱引用 %@", o);

无论是否启用 Zombies,都会发生相同的输出:

<前>2013-12-05 23:55:35.571 WeakPointers [93840:303] 发布前弱参考2013-12-05 23:55:35.572 WeakPointers[93840:303] 发布后弱引用(空)

Normally, if an object deallocated, all weak references will be nilled.

What about if we enable zombie object?

Product -> Edit Scheme -> Enable Zombie Object

Will weak references be nilled too?

Otherwise, bugs that won't happen on normal circumstances will happen on enable zombie object

I strongly suspected, as one of the answer says, that weak references will be nilled.

That being said,

How to fix the bug where viewDidScroll is called when UIViewController is already gone? and answer there suggest otherwise.

解决方案

The weak reference will be set to nil even if you enable Zombie Objects.

I tested it like this:

id __weak o;
@autoreleasepool {
    id ob = [[NSObject alloc] init];
    o = ob;
    NSLog(@"Weak ref before release %@", o);
}
NSLog(@"Weak ref after release %@", o);

With Zombies enabled or not, the same output happens:

2013-12-05 23:55:35.571 WeakPointers[93840:303] Weak ref before release  
2013-12-05 23:55:35.572 WeakPointers[93840:303] Weak ref after release (null)

这篇关于如果我们启用僵尸对象,所有弱引用都会被清零吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆