Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How correctly release UITabController

This may sound a newbie question ... but however I'm new to iOS development.

I've created UITabController object programmatically like this.

mTabBarController = [[UITabBarController alloc] init];
...
mTabBarController.viewControllers = [NSArray arrayWithArray:tabBarItems];
[tabBarItems release];

And releasing mTabBarController in dealloc like this.

- (void)dealloc {
    [mTabBarController release];
}

Now my question : will I get a memory leak ? When I assign value t viewController the ref count of tabBarItems is still 1. When I release mTabBarController does it also release all its viewcontrollers ?

like image 431
deimus Avatar asked Mar 22 '26 10:03

deimus


2 Answers

Yes, the tab controller owns an array of view controllers (and everything in an array is retained). You're not creating a leak as long as you properly release or autorelease the items you're adding to the tabBarItems array.

It really helps to think of object relationships as ownerships.

like image 93
Ash Furrow Avatar answered Mar 24 '26 01:03

Ash Furrow


UITabBarController should never be placed as the child of another ViewController, so you will always have to release it in dealloc. If your TabBarController's view is the childview of your application's window, it's ok not to release it in dealloc since the only time dealloc will ever get called is when your program is closing, in which case your controller will be released anyways. However, some people like to release it in dealloc anyways just to keep their code consistent. What you're doing is fine.

like image 45
aleph_null Avatar answered Mar 24 '26 00:03

aleph_null



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!