1

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?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

3 Answers3

1

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

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
1

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.
  • 33,468
  • 5
  • 78
  • 96
-1

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
  • 6,026
  • 2
  • 34
  • 45
  • 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. – Stefan Arentz Aug 20 '10 at 00:55
  • 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 Aug 20 '10 at 05:52