Strange ARC issue not releasing ivar in UIView subclass [duplicate]

梦想与她 提交于 2019-12-03 15:17:59
mattjgalloway

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;
}

That should output:

ClassA dealloc
ClassB dealloc

But with NSZombieEnabled set to YES, it outputs:

ClassA dealloc

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.

Edit: 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!