views:

45

answers:

4

I have some view controllers:

StockTwitsTVViewController* stvViewController = [[[StockTwitsTVViewController alloc] initWithNibName:@"StockTwitsTVViewController" bundle:nil]autorelease];
    UINavigationController *stvNavController = [[UINavigationController alloc] initWithRootViewController:stvViewController];
    stvNavController.tabBarItem.image = [UIImage imageNamed:@"today-icon.png"];

    ScheduleViewController* scheduleViewController = [[[ScheduleViewController alloc] initWithNibName:@"ScheduleViewController" bundle:nil]autorelease];
    scheduleViewController.title = @"Archives";
    UINavigationController *scheduleNavController = [[UINavigationController alloc] initWithRootViewController:scheduleViewController];
    scheduleNavController.tabBarItem.image = [UIImage imageNamed:@"archived-icon.png"];

    stvTabBarController = [[UITabBarController alloc] init];
    stvTabBarController.delegate = self;
    stvTabBarController.viewControllers = [NSArray arrayWithObjects:stvNavController, scheduleNavController, nil];
    stvTabBarController.selectedViewController = stvNavController;

    [stvNavController release];
    [scheduleNavController release];

    [self presentModalViewController:stvTabBarController animated:YES];

Is it ok to auto-release them or is it better practice to manually release? Why?

A: 

In a resource sparse platform, like the iPhone, more control over your memory management is better. As the designer, you want to have as much free memory as you can, so you don't want anything hanging around longer than it needs to. Once you have added the view controller to the navigationController, take direct control of it's life cycle and release it. This is the generally accepted best practice. See this page for a good explanation of what autorelease does. In your case, I wouldn't use it.

http://macdevcenter.com/pub/a/mac/2001/07/27/cocoa.html?page=3

Drewsmits
@St3fan, what he is doing may be perfectly fine in this case, but as a habit, it is best to try and take control of an objects life cycle, IMHO. Sure, you run the risk of leaking memory, but that is usually easier to fix than a random and non-descript autorelease related crash. This is a debate that lots of other people have had, and I'm not sure presenting my opinion is worth a down vote.http://stackoverflow.com/questions/193288/what-is-the-cost-of-using-autorelease-in-cocoa
Drewsmits
A: 

Hi Sheehan,

The autorelease will let the release to be done later by the OS, it means when the current run loop finishes.

The release inform the OS that the object id not necessary anymore and that the allocated memory can be freed.

For performance on iOS it's better to use release, instead of autorelease because the system can claim back the allocated memory straightaway.

vfn
It is not really a valid argument in this case as the memory will not be claimed back at all in this case since all objects created will be immediately owned by some other object.
St3fan
Because of that I said "the system can claim", I didn't said the the system will. The system will claim the memory back ass soon as the retain count for the object reaches zero. Using autorelease even with a retain count == 0, the system won't claim the memory back until the current run loop ends. What about if the view controller is dismissed before the block of code ends?
vfn
+1  A: 

What you do in your code is perfectly fine. To make things more consistent I would also create stdNavController and scheduleNavController as autoreleased objects.

St3fan
+1  A: 

Read mikeash.com: Autorelease is Fast.

What he didn't test was autorelease versus release. When I tested, a million [[[NSObject alloc] init] autorelease] plus the autorelease pool overhead took on the order of twice as long as [[[NSObject alloc] init] release]. Admittedly, I tested on 10.6 (presumably if it's still refcounted since I didn't enable GC), but the relative performance still holds.

Maybe autorelease uses a couple of microseconds of CPU time, but it sure beats adding a memory leak because you changed an ivar to a local, or you copy-pasted code around and forgot the release.

Care about performance when it matters. When it does, you might decide to use CFString instead of NSString and ivar access instead of property access and function calls instead of class methods. In general, though, it's important to write code that is easy to maintain, even if that means using 1% more CPU.

tc.