奇怪的ARC问题没有在UIView子类中释放ivar [英] Strange ARC issue not releasing ivar in UIView subclass
问题描述
Possible Duplicate:
Why is object not dealloc'ed when using ARC + NSZombieEnabled
我在项目中遇到了一个非常奇怪的问题。简单地说,我有 ViewA
,它拥有 ViewB
( strong
属性)。 ViewA
在其初始化程序中创建 ViewB
。这两个对象都是 UIView
的子类。
I've got a very strange issue I'm seeing at the moment in a project. Put simply I have ViewA
which owns ViewB
(strong
property). ViewA
creates its ViewB
in its initialiser. Both objects are subclasses of UIView
.
我已覆盖 dealloc
在两者中并放置一个日志行和一个断点以查看它们是否被击中。似乎 ViewA
的 dealloc
正在被点击但不是 ViewB
的。但是,如果我在 ViewA的
然后它 点击。 dealloc
中输入 self.viewB = nil
I have overridden dealloc
in both and put a log line and a break point to see if they get hit. It seems that ViewA
's dealloc
is being hit but not ViewB
's. However if I put in a self.viewB = nil
in the dealloc
of ViewA
then it is hit.
所以基本上它是这样的:
So basically it's something like this:
@interface ViewA : UIView
@property (nonatomic, strong) ViewB *viewB;
@end
@implementation ViewA
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.viewB = [[ViewB alloc] initWithFrame:self.bounds];
[self addSubview:self.viewB];
}
return self;
}
- (void)dealloc {
//self.viewB = nil; ///< Toggling this commented/uncommented changes if ViewB's dealloc gets called.
NSLog(@"ViewA dealloc");
}
@end
我无法理解为什么nil-ing viewB
out会有所不同。如果其他东西保留在 viewB
那么它应该完全没有区别,如果我把它弄出来或不在这里。并且它不应该对ARC添加的版本数量产生影响。
What I can't understand is why nil-ing viewB
out makes a difference. If something else is holding onto viewB
then it should make absolutely no difference if I nil it out or not here. And it shouldn't make a difference to the number of releases that ARC adds in either.
我似乎无法在最小的测试用例中重现它,但我正在努力。不幸的是,我无法发布我看到的实际代码。我不认为这是一个问题,因为更重要的是,将它弄清楚不应该让我感到困惑。
I can't seem to reproduce it in a minimal test case as yet, but I'm working on it. And I can't post the actual code I'm seeing this in unfortunately. I don't see that being an issue though because it's more the point that nil-ing it out shouldn't make a difference that I am confused by.
任何人都可以看到我忽略的任何东西或提供有关在哪里寻找调试此问题的建议?
Can anyone see anything I am overlooking or give advice about where to look for debugging this problem?
更新:
我发现了问题。当 NSZombieEnabled
设置为 YES
时,它似乎只是一个问题。那是完全疯了,肯定是一个错误。据我所知,僵尸不应该影响它的运作方式。对象仍应通过 dealloc
方法。更重要的是,如果我在 ViewA
的
。 viewB
> dealloc
I've found the problem. It appears that it's only a problem when NSZombieEnabled
is set to YES
. Well that is entirely mad and has to be a bug surely. Zombies should not affect how this works as far as I know. The objects should still go through the dealloc
method. And what's more, it's just mad that it works if I nil out viewB
in ViewA
's dealloc
.
推荐答案
我发现这似乎是iOS实现中的一个错误僵尸请考虑以下代码:
I've found that this appears to be a bug in the iOS implementation of zombies. Consider the following code:
#import <Foundation/Foundation.h>
@interface ClassB : NSObject
@end
@implementation ClassB
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (void)dealloc {
NSLog(@"ClassB dealloc");
}
@end
@interface ClassA : NSObject
@property (nonatomic, strong) ClassB *b;
@end
@implementation ClassA
@synthesize b;
- (id)init {
if ((self = [super init])) {
b = [[ClassB alloc] init];
}
return self;
}
- (void)dealloc {
NSLog(@"ClassA dealloc");
}
@end
int main() {
ClassA *a = [[ClassA alloc] init];
return 0;
}
应输出:
ClassA dealloc
ClassB dealloc
但是 NSZombieEnabled
设置为 YES
,输出:
ClassA dealloc
据我所知,这是一个错误。它似乎只发生在iOS(模拟器和设备)上,并且在为Mac OS X构建和运行时不会发生。我已经向Apple提交了雷达。
As far as I can tell, this is a bug. It seems to only happen with iOS (both simulator and device) and does not happen when built and run for Mac OS X. I've filed a radar with Apple.
编辑:事实证明这已经在这里得到了解答 - 使用ARC + NSZombieEnabled时为什么对象没有被释放?在我发现真正的问题之后管理找到它。顺便说一句,它与ARC无关。
It turns out this has already been answered here - Why is object not dealloc'ed when using ARC + NSZombieEnabled . Managed to find it after I found out what the real problem was. It's nothing to do with ARC by the way.
这篇关于奇怪的ARC问题没有在UIView子类中释放ivar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!