releasing view controllers in apple example code
Over here http://developer.apple开发者_开发百科.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html
at listing 4.1
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
The view controllers are alloced but not released. Shouldn't there be a release of vc2 and vc2 after they've been added to the array?
Unless I'm missing something, you're right that vc1 and vc2 should be released. The method you posted allocs the view controllers, so it should either save them in instance variables so that they can be released later, or it should go ahead and release them after adding them to the array.
In a practical sense, it really doesn't matter much, however. In a tab-based app, the view controllers associated with the tab controller usually stick around for the life of the app. When the app terminates, those objects will be cleaned up anyway. This also isn't quite a leak, since the tab controller still has references to the objects pointed to by vc1 and vc2, but it's about as close to leaking as you can get without becoming a true leak.
Good eyes, though. You should consider filing a bug with Apple about that.
精彩评论