Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bengimizrahi

macrumors newbie
Original poster
May 24, 2008
13
0
Istanbul
Instead of:

Code:
UIWindow *localPortraitWindow;
localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.portraitWindow = localPortraitWindow;

can we do the following?:

Code:
self.portraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.portraitWindow release];

In the second one, [UIWindow alloc] increments reference count by 1;
'self.portraitWindow = ...' increments by 1, too. Finally, [self.portraitWindow release] decrements by 1.

Am I correct?
 

cmaier

Suspended
Jul 25, 2007
25,405
33,474
California
Instead of:

Code:
UIWindow *localPortraitWindow;
localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.portraitWindow = localPortraitWindow;

can we do the following?:

Code:
self.portraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.portraitWindow release];

In the second one, [UIWindow alloc] increments reference count by 1;
'self.portraitWindow = ...' increments by 1, too. Finally, [self.portraitWindow release] decrements by 1.

Am I correct?

i assume you didn't declare the portraitWindow property as "assign?"
 

therevolution

macrumors 6502
May 12, 2003
468
0
Let me start of by saying I'm somewhat new to Objective-C, so I don't claim to be an expert. I probably shouldn't be giving out advice yet. ;) Someone please correct me if I'm wrong.

I'm assuming self.portraitWindow is a property with 'retain' specified. In that case, BOTH #1 and #2 up the reference count when 'self.portraitWindow = ...' is invoked. Not just #2. So if the assumption is correct, then yes, you should call release. But you should be doing it in either case.

If you're really just out to lower your lines of code count, I propose this:

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

bengimizrahi

macrumors newbie
Original poster
May 24, 2008
13
0
Istanbul
Oh, the property has "retain" attribute, I forgot to mention.

So, in this case, all examples are correct including your 'autorelease' version. What can be the disadvantage of using 'autorelease' in this example?
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
Code:
UIWindow *localPortraitWindow;
localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.portraitWindow = localPortraitWindow;

This is not correct. You're implicitly retaining the object with the initWithFrame: method, and again with the assignment to the portraitWindow property. This must be balanced with a release or autorelease.

Code:
self.portraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.portraitWindow release];

This is technically correct, but I would consider it bad coding practise to manually release a property in this way. There's nothing wrong with this at all, I just don't like the semantics.

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

This is what I'd do in most circumstances. The only downside to this is a slight performance penalty involved in autorelease over release, but this is only something worth worrying about if the code is being run hundreds or thousands of times a second (which this particular code obviously isn't).

If I wanted to avoid autorelease, this is what I'd do:

Code:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.portraitWindow = window;
[window release];
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
If UIWindow is your own class, the typical Cocoa idiom would be to add a class method to return an instance already autoreleased (a convenience method), and to use that:

Code:
self.portraitWindow = [UIWindow windowWithFrame:[[UIScreen mainScreen] bounds];

If UIWindow is not your own class, use Nutter's third code snippet; autorelease is exactly the right thing to do here.

On that note, I rather wish Apple hadn't allowed a retain attribute on properties. Yes, it makes things simpler in some cases (specifically where you're using a lot of convenience methods returning autoreleased objects), but in cases where classes DON'T have convenience methods, it introduces headaches like this.
 

bengimizrahi

macrumors newbie
Original poster
May 24, 2008
13
0
Istanbul
Thank you all.

So here is what I've learned:

Use the following semantic as much as possible:
Code:
UIWindow *localPortraitWindow;
localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.portraitWindow = localPortraitWindow;
[localPartraitWindow release];

Using the following one is ok, but it does not emphisaze that there were actually 2 retains:
Code:
self.portraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.portraitWindow release];

If the code fragment is not repetitively executed, it is ok to do the following:
Code:
self.portraitWindow = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.