开发者

release or autorelease?

开发者 https://www.devze.com 2023-01-24 09:59 出处:网络
should i use release or autorelease for varSecondViewController? -(IBAction)takeNextStep: (id) 开发者_如何学运维sender

should i use release or autorelease for varSecondViewController?

-(IBAction)takeNextStep: (id) 开发者_如何学运维sender
{
    SecondViewController *varSecondViewController = [[SecondViewController alloc]     
       initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:varSecondViewController animated:YES];
    [varSecondViewController release];
}


My rule of thumb:

If you're going to use it, and then no longer need a reference to it, release it,

If you're going to pass it back to the caller (i.e. return it), autorelease it.


autorelease is just a release that's delayed until some time in the future, which is guaranteed to be at least the current call stack unless a caller has created its own autorelease pool. You generally use it when you need to release an object in order to follow the memory management guidelines, but that object might still be needed further up the call stack. In this case, you're not returning the view controller and have no intention of directly holding onto it any further, so there's no need for a delay. You can just release.


In this case, release makes the most sense.

0

精彩评论

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