Sheehan Alam
Sheehan Alam

Reputation: 60919

Is it ok to autorelease UIViewControllers?

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?

Upvotes: 1

Views: 651

Answers (3)

tc.
tc.

Reputation: 33602

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.

Upvotes: 1

Stefan Arentz
Stefan Arentz

Reputation: 34935

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

Upvotes: 1

vfn
vfn

Reputation: 6066

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.

Upvotes: -1

Related Questions