下面是使用ARC的OS X的Objective-C程序 - 您可以使用cc -fobjc-arc -o objc_arc_test objc_arc_test.m
或其他东西来构建它。它创建两对两个对象,一个使用alloc/init,另一个使用工厂函数(在ARC代码中使用autorelease
),所有autorelease池之外,都会打印init和dealloc消息去。为什么这些ARC对象的行为不一致?
#import <Foundation/NSObject.h>
#include <stdio.h>
#if !__has_feature(objc_arc)
#error
#endif
@interface TestClass:NSObject {
int value;
}
-(TestClass *)initWithValue:(int)value;
-(void)dealloc;
+(TestClass *)testClassWithValue:(int)value;
@end
@implementation TestClass
-(TestClass *)initWithValue:(int)value_ {
if((self=[super init]))
self->value=value_;
printf("init: self=%p value=%d\n",self,self->value);
return self;
}
-(void)dealloc {
printf("dealloc: self=%p value=%d\n",self,self->value);
}
+(TestClass *)testClassWithValue:(int)value {
return [[TestClass alloc] initWithValue:value];
}
@end
static void f() {
TestClass *c5=[TestClass testClassWithValue:5];
TestClass *c6=[[TestClass alloc] initWithValue:6];
}
static void f2() {
TestClass *c7=[TestClass testClassWithValue:7];
TestClass *c8=[[TestClass alloc] initWithValue:8];
}
int main() {
f();
f2();
}
我预料得到的init消息4个对象和dealloc的消息2,因为ARC将确保ALLOC + init'd对象被销毁,并在帐户缺乏自动释放池,将单独留下其他人。
但我得到的却是4个初始化对象的消息,并dealloc的消息3:
init: self=0x7fea20500690 value=5
init: self=0x7fea205006f0 value=6
dealloc: self=0x7fea205006f0 value=6
init: self=0x7fea205006f0 value=7
init: self=0x7fea20500700 value=8
dealloc: self=0x7fea20500700 value=8
dealloc: self=0x7fea205006f0 value=7
我不明白这种行为!我期望值= 5和值= 7对象表现相同。
它为什么这样做?
(OS X 10.11.6; Xcode的8 - Apple LLVM version 8.0.0 (clang-800.0.38)
,Target: x86_64-apple-darwin15.6.0
,Thread model: posix
)