[英]Why is window autoreleased in appliciation:didFinishLaunchingWithOptions: and released in dealloc?
I have created an iphone application using the empty template without ARC in xcode 4.2. 我使用xcode 4.2中没有ARC的空模板创建了iphone应用程序。 I'm not currently using ARC because I want to learn the basics of reference counting.
我目前不使用ARC,因为我想学习引用计数的基础。 In the application delegate I have the following method:
在应用程序委托中,我具有以下方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Why is window
autoreleased? 为什么
window
自动释放? Is it because AppDelegate won't be using it in the future? 是否因为AppDelegate将来不再使用它? But it is being assigned to a instance variable.
但是它被分配给一个实例变量。 There is also a dealloc method where
window
is released. 还有一个释放
window
的释放方法。 Why is it released when it is already autoreleased? 为什么已经自动发布,为什么会发布?
- (void)dealloc
{
[_window release];
[super dealloc];
}
The property of the window
in .h
file is declared as @property (nonatomic, retain) UIWindow *window;
.h
文件中window
的属性声明为@property (nonatomic, retain) UIWindow *window;
. 。 The
window
has a retain
property. window
具有retain
属性。 So the UIWindow
is retained by the setter method of the window
variable. 因此,
UIWindow
由window
变量的setter方法保留。 In the line self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
在这一行中
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
the new window
alloc
ed has +1
in retainCount
because of the alloc
and another +1
because of the window
setter method resulting in a +2
retainCount
. 新
window
alloc
编有+1
在retainCount
因为的alloc
和另一+1
因为的window
导致setter方法+2
retainCount
。 The autorelease
is to decrease the retainCount
back to +1
. autorelease
是将retainCount
减少回+1
。 In the dealloc
the retainCount
goes to 0
and the window
is deallocated. 在
dealloc
的retainCount
变为0
和window
被释放。
Every retain
, alloc
, copy
and new
, must be balanced by release
or autorelease
. 每个
retain
, alloc
, copy
和new
,必须通过release
或autorelease
release
来平衡。
So in you're code: 所以在你的代码:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
The alloc
is balanced by the autorelease
. alloc
由autorelease
平衡。
Now for the release
in dealloc, if you look at the definition of the window
property, you will see it is a retained proerty: (in your AppDelegate header) 现在,对于dealloc中的
release
,如果您查看window
属性的定义,您将看到它是保留的属性:(在AppDelegate标头中)
@property (retain, nonatomic) UIWindow *window;
or the more modern equivalent: (where the strong means retain in this case) 或更现代的等价形式:(在这种情况下,强有力的手段仍然存在)
@property (strong, nonatomic) UIWindow *window;
This means you know have one outstanding retain
, as the @property and @synthesize is there to stop you have to write boilerplate code over and over again. 这意味着您知道有一个出色的
retain
,因为@property和@synthesize在那阻止您必须一遍又一遍地编写样板代码。
So this must be balanced by the release
in the dealloc
method: 因此,这必须通过
dealloc
方法中的release
来平衡:
- (void)dealloc
{
[_window release];
[super dealloc];
}
window
is retained by the property, so when creating it you shouldn't leave t retained (which is what alloc/init does). window
由该属性保留,因此在创建它时,您不应保留t(这是alloc / init所做的)。 It is autoreleased because it is easier than just releasing (releasing would work as well). 它是自动发布的,因为它比发布更容易(释放也可以)。 It has to be release in dealloc to counter the retain nature of the property.
它必须在释放时释放,以抵消该属性的保留性质。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.