Possible Duplicate:
Why is object not dealloc'ed when using ARC + NSZombieEnabled
In a project using ARC with zombie objects enabled, if an object holds a strong reference to another object when it is deallocated, that other object does not get released.
This code demonstrates the problem:
@interface TestInner : NSObject
@end
@implementation TestInner
- (id)init {
if ( (self = [super init]) ) {
}
NSLog(@"-[TestInner init]: %p", self);
return self;
}
- (void)dealloc {
NSLog(@"-[TestInner dealloc]: %p", self);
}
@end
@interface TestOuter : NSObject
@end
@implementation TestOuter {
TestInner * _inner;
}
- (id)init {
if ( (self = [super init]) ) {
_inner = [[TestInner alloc] init];
}
NSLog(@"-[TestOuter init]: %p", self);
return self;
}
- (void)dealloc {
NSLog(@"-[TestOuter dealloc]: %p", self);
// _inner will not release without this if zombies are enabled
//_inner = nil;
}
@end
Allocating and releasing a TestOuter instance yields the following log messages:
-[TestInner init]: 0x9d00f00
-[TestOuter init]: 0x9d00f30
-[TestOuter dealloc]: 0x9d00f30
The TestInner instance is never deallocated. Turning off zombies will, however, cause the TestInner instance to be deallocated.
One of the benefits of switching to ARC was that we so rarely needed to implement a dealloc method simply to release held object references. It seems that with zombies enabled, which is useful to have to catch all kinds of mistakes during development, that this benefit cannot be realized. I will still need to implement dealloc methods just to set my held references to nil
.
Am I missing something here, or is this really the expected behavior when mixing ARC with zombies?