简体   繁体   English

为什么窗口在appliciation:didFinishLaunchingWithOptions:中自动释放,而在dealloc中释放?

[英]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. 因此, UIWindowwindow变量的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编有+1retainCount因为的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. deallocretainCount变为0window被释放。

Every retain , alloc , copy and new , must be balanced by release or autorelease . 每个retainalloccopynew ,必须通过releaseautorelease release来平衡。


So in you're code: 所以在你的代码:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

The alloc is balanced by the autorelease . allocautorelease平衡。


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.

相关问题 视图卸载和取消分配后,iPhone内存未释放,为什么? - iPhone memory not released after view unloads and dealloc, why? iPhone .. NSString被释放和释放-但是我不知道在哪里或为什么 - iPhone .. NSString being released & dealloc — but I cannot tell where or why 什么时候释放自动释放的对象? - When is an autoreleased object actually released? NSMutableArray 元素未在 dealloc 中释放 - NSMutableArray elements not being released in dealloc 为什么我看到一些代码在 viewDidUnload 中而不是在 dealloc 中释放了东西? - why do I see some code where stuff is released in viewDidUnload as opposed to dealloc? 在保留,分配和自动释放应用程序后,在dealloc中释放UILabel会使应用程序崩溃 - releasing a UILabel in dealloc crashes the app after it was retained, allocated & autoreleased Objective- C防止NSString被释放/释放 - Objective- C prevent NSString from being released/ dealloc 当使用+ imageNamed创建的uiimage时,应用程序崩溃:在dealloc中发布 - app crashing when uiimage created with +imageNamed: is released in dealloc 是否必须在dealloc中释放非指针实例变量? - Must non-pointer instance variables be released in dealloc? 为什么在应用程序中显示UIAlertView:didFinishLaunchingWithOptions:导致错误? - Why is showing a UIAlertView in application:didFinishLaunchingWithOptions: causing an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM