开发者

is this lead to memory leak for iphone

Let me explain in detais

In appDidfinish()
{
preLoginNavController  = [[PreLoginNavController alloc] initPreLoginNavController];
[window addSubview:[preLoginNavControlle开发者_运维问答r  view]];
}

then in preLoginViewController when user press a button

then i am doing this to go to view2

RootViewController *arootController= [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
  [self.navigationController pushViewController:arootController animated:YES];

if i do this [arootController release]; then i cant come form view 2

now in view 2 when back button is pressed then i am doing this

[self.navigationController popToRootViewControllerAnimated:YES];

so i cannot release [arootController release] else when i go to back view app quits with no error

and i need a prelogin view before Rootview thats why i did like that now what should i do .. my app is working fine but i want to fix that leak :(

HEY i am getting this message when i click back button in view 2 after push and release in preLogin(1st view) objc[408]: FREED(id): message release sent to freed object=0x466a340


I think yes, is leaking arootController once you pop it.


yeah, there will have a leak. 2 suggested solutions are:

[arootController autorelease];

or after you do :

[self.navigationController pushViewController:arootController animated:YES];

you can release it.

A good practice is that : who increase the retain Count, should decrease it. and because aRootController is init in that class, it should be released there

Edit:

This should be the correct code if you want to use navigationController:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    self.preLoginNavController = [[[PreLoginNavController alloc] init] autorelease];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainItemListViewController];
    [window addSubview:[self.navigationController view]];
    [window makeKeyAndVisible];  
}

then when you need to push:

  [self.navigationController pushViewController:anotherViewController animated:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜