iOS memory leak on app loading - iphone

I'm trying to find a memory leak i have on my app. When the application loads i instantly get a memory leak that looks like that in instuments
how can i debug that ? it doesn't reproduce it self no matter how long i run my app or what i do, only when the applications loads.
and here is my code in the delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
tabBarController = [[UITabBarController alloc] init];
search = [[iPhoneView alloc] initWithNibName:#"iPhoneView" bundle:nil];
homeNavigationController = [[UINavigationController alloc] initWithRootViewController:search];
favouritesNavigationController = [[UINavigationController alloc] init];
favoritesViewController = [[FavoritesViewController alloc]init];
[favouritesNavigationController pushViewController:favoritesViewController animated:NO];
aboutUsViewController =[[AboutUsViewController alloc] init];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"toolbox" image:[UIImage imageNamed:#"aboutus"] tag:0];
aboutUsViewController.tabBarItem = item;
[item release];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#"αγαπημένα" image:[UIImage imageNamed:#"favorites"] tag:0];
favouritesNavigationController.tabBarItem = item2;
[item2 release];
NSArray *tabBarControllerCollection = [NSArray arrayWithObjects:homeNavigationController,favouritesNavigationController,aboutUsViewController,nil];
[tabBarController setViewControllers:tabBarControllerCollection animated:NO];
[window setRootViewController:tabBarController];
[tabBarControllerCollection release]; //added that for the leaks
//[window addSubview:tabBarController.view]; for the warning thing about window and root view controller
[window makeKeyAndVisible];
}
- (void)dealloc {
[tabBarController release];
[search release];
[favoritesViewController release];
[favouritesNavigationController release];
[aboutUsViewController release];
[window release];
[super dealloc];
}

Please use the Call Tree view of your Leaks, that will show where the problem happened and will help you troubleshoot. Also try with a real device as the simulator might show false-positives.

Release homeNavigationController which not released and check if you are missing somewhere else.

Related

Load a tabbarcontroller when back button is clicked in xcode?

When i click on back button then Im loading a particular view.But to that particular view I have to add a tabbarController.How can i do it..?When i try to add it doesnot get added..Couldnot understand y ?
-(IBAction)switchtofirst {
AppViewController *first=[[AppViewController alloc] initWithNibName:#"AppViewController" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
AppViewController *gp=[AppViewController alloc];
[gp.view addSubview:tabBarController.view];
[self presentModalViewController:gp animated:NO];
[gp release];
}
Try something like this:
-(IBAction)switchtofirst {
AppViewController *first = [[AppViewController alloc] initWithNibName:#"AppViewController" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray= [NSArray arrayWithObjects:first, second, nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[self presentViewController:tabBarController animated:YES completion:nil];
}

UITabBar Title Not Showing

I've implemented a UITabBar programmatically exactly the way Apple recommends, and I can't get the title to show up. Here is how I'm implementing it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = [[UITabBarController alloc] init];
MainViewController *mainViewController = [[MainViewController alloc] init];
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
[[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];
[tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]];
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
[mainViewController release];
[mainNavigationController release];
[tabBarController release];
return YES;
}
Here is the init method of mainViewController where I add the title:
#implementation MainViewController
- (id) init
{
self = [super initWithNibName: nil bundle: nil];
if (self)
{
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Main" image: nil tag: 1];
[self setTabBarItem: tabBarItem];
[tabBarItem release];
}
return self;
}
I noticed there are a bunch of other questions on this, and some have hacky type solutions, but I'm wondering if Apple is recommending this way, why isn't it working??
This can be done in a variety of ways. The simplest one is to try setting
self.title = #"Main";
This should be reflected in both your navigationBar title and tabBarItem title.
Or , you could just connect them to the interface builder and type it out yourself and connect the outlets properly.

how to release a tabbarcontroller?

A UITabBarController's viewControllers are navigationController, when I release the tabbarcontroller, I found the memory would not released ?
You cant release them if you create them in .xib file. They will be released then your application end work !!!
If you are working in code you probably need to release the navigationControllers after adding them to the tabBarController...
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *controllerArray = [[NSMutableArray alloc] initWithCapacity:2];
UINavigationController *localNavigationController;
AccountViewController *accountViewController = [[AccountViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:accountViewController];
[controllerArray addObject:localNavigationController];
[localNavigationController release];
[accountViewController release];
AccountHistoryViewController *accountHistoryViewController = [[AccountHistoryViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:accountHistoryViewController];
[controllerArray addObject:localNavigationController];
[localNavigationController release];
[accountHistoryViewController release];
[tabBarController setViewControllers:controllerArray];
[controllerArray release];
you should release them in dealloc method of app delegate class
- (void)dealloc {
[tabbarcontroller release];
[window release];
[super dealloc];
}

How to create a tab bar controller with presents a nil modal view

i am creating an application which make uses of tabbar.i have created a function named setuptabbar which calls all the classes that use tab bar . this my code.
-(void)setupTabbar{
//this is used to allocate space for tab bar item.
TJourneylistController *journeylist =[[TJourneylistController alloc]initWithNibName:nil bundle:nil];
UINavigationController *journeylistnavigation =[[UINavigationController alloc]initWithRootViewController:journeylist];
[journeylist.tabBarItem initWithTitle:#"Journey List" image:[UIImage imageNamed:#""] tag:2];
[journeylist release];
TAppStoreController *appstore =[[TAppStoreController alloc]initWithNibName:nil bundle:nil];
UINavigationController *appstorenavigation =[[UINavigationController alloc]initWithRootViewController:appstore];
[appstore.tabBarItem initWithTitle:#"App Stroe" image:[UIImage imageNamed:#""] tag:1];
[appstore release];
TSettingsController *settings =[[TSettingsController alloc]initWithNibName:nil bundle:nil];
UINavigationController *settingsnavigation =[[UINavigationController alloc]initWithRootViewController:settings];
[settings.tabBarItem initWithTitle:#"Journey List" image:[UIImage imageNamed:#""] tag:3];
[settings release];
TAboutController *about =[[TAboutController alloc]initWithNibName:nil bundle:nil];
UINavigationController *aboutnavigation =[[UINavigationController alloc]initWithRootViewController:about];
[about.tabBarItem initWithTitle:#"Journey List" image:[UIImage imageNamed:#""] tag:4];
[about release];
mTabController.viewControllers = [[NSArray alloc] initWithObjects:appstorenavigation,journeylistnavigation,settingsnavigation,aboutnavigation,nil];
[appstorenavigation release];
[journeylistnavigation release];
[settingsnavigation release];
[aboutnavigation release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
mTabController = [[UITabBarController alloc]init];
[self setupTabbar];
if([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil)
mTabController.selectedViewController = [mTabController.viewControllers objectAtIndex:0];
[mTabController presentModalViewController:mViewController animated:YES];
//Screen *screen =[[Screen alloc]initWithNibName:#"Screen" bundle:nil];
[window addSubview:mTabController.view];
[self.window makeKeyAndVisible];
//[screen release];
return YES;
}
But when i run the project it gives me an error that 'Application tried to present a nil modal view controller.What is the problem.
In you following line:
[mTabController presentModalViewController:mViewController animated:YES];
You are presenting mViewController from mTabController. What is mViewController ? It's maybe because mViewController is never allocated. If you remove this line I think your code is working.

Setting a navigationBar title

I currently have my navigation controllers defined in my appDelegate as followed (code summarized):
- (void) applicationDidFinishLaunching {
tabBarController = [[UITabBarController alloc] init];
FlagList *flagList = [[FlagList alloc] initWithApiCall:API_PUBLICTIMELINE andTitle:#"Home"];
UITabBarItem *homeTab = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home.png"]
tag:0];
flagList.tabBarItem=homeTab;
[homeTab release];
tabBarController.viewControllers=[NSArray arrayWithObjects:flagList,nil];
[flagList release];
[rootViewController release];
rootViewController = [[UINavigationController alloc] initWithRootViewController:[tabBarController autorelease]];
rootViewController.navigationBar.barStyle=UIBarStyleDefault;
}
I want to set a title in the navigationBar of my FlagListView. HOWEVER, I want to be able to do this in the -viewDidLoad method of my FlagList UITableViewController class. How can I access this property?
I tried:
[[self navigationItem] setTitle:#"Home"];
..but it doesn't seem to work. Can someone please tell me what I'm doing wrong?
Assuming FlagList is a descendant if ViewController use [self setTitle:#"Home"] instead of [[self navigationItem] setTitle:#"Home"];
[self setTitle:#"Home"];
This should work.