PDA

View Full Version : Autorelease question




zrbecker
Jul 1, 2009, 11:38 AM
Is using autorelease a lot of overhead for iPhone applications?

Is it enough overhead to worry about the following...

// This gives me a NSString that will autorelease. (I think)
NSString *string1 = [NSString stringWithFormat:@"%@, %@",
lastName, firstName];

NSString *string2 = [[NSString alloc] initWithFormat:@"%@, %@",
lastName, firstName];

// Do some stuff with strings

[string2 release];

Would string2 be better/worse implementation? Or does it not matter and I am worrying too much about it.

Thanks!

I am new to iPhone Programming and Objective-C.



PhoneyDeveloper
Jul 1, 2009, 11:52 AM
I wouldn't worry about it too much. The main issue is probably if you create autoreleased objects in a loop. If the code you showed were in a loop executed 1000x then I would do my best to not use autoreleased strings.

In general, if it is simple to use an alloc/inited/released object then do so. This is considered the 'best practice.' If you are returning objects from methods then they should almost always be autoreleased. If you create a few autoreleased strings here and there it won't have a noticeable impact on your app.