Autorelease question

Discussion in 'iOS Programming' started by zrbecker, Jul 1, 2009.

  1. zrbecker macrumors member

    Joined:
    Jun 28, 2009
    #1
    Is using autorelease a lot of overhead for iPhone applications?

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

    Code:
    // 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.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     

Share This Page