jorurmol
jorurmol

Reputation: 117

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. I'm not currently using ARC because I want to learn the basics of reference counting. 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? Is it because AppDelegate won't be using it in the future? But it is being assigned to a instance variable. There is also a dealloc method where window is released. Why is it released when it is already autoreleased?

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

Upvotes: 2

Views: 2143

Answers (3)

Bruno Domingues
Bruno Domingues

Reputation: 1015

The property of the window in .h file is declared as @property (nonatomic, retain) UIWindow *window;. The window has a retain property. So the UIWindow is retained by the setter method of the window variable. In the line self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; the new window alloced has +1 in retainCount because of the alloc and another +1 because of the window setter method resulting in a +2 retainCount. The autorelease is to decrease the retainCount back to +1. In the dealloc the retainCount goes to 0 and the window is deallocated.

Upvotes: 6

Jonathan.
Jonathan.

Reputation: 55594

Every retain, alloc, copy and new, must be balanced by release or autorelease.


So in you're code:

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

The alloc is balanced by the 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)

@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.

So this must be balanced by the release in the dealloc method:

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

Upvotes: 1

jrtc27
jrtc27

Reputation: 8536

window is retained by the property, so when creating it you shouldn't leave t retained (which is what alloc/init does). 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.

Upvotes: 0

Related Questions