简体   繁体   中英

iPhone memory management

Do I need to call release here or not?

I am Loading view in didSelectRowAtIndexPath in tableview...

EventDetailedViewController *eventDetailedViewController=[[EventDetailedViewController alloc]initWithNibName:@"EventDetailedViewController" bundle:nil];
        eventDetailedViewController.aEventInfo=aEventInfo;
        [self.navigationController pushViewController:eventDetailedViewController animated:YES];
    //  [eventDetailedViewController release];
        eventDetailedViewController=nil;

When I need to call [eventDetailedViewController release]; and when I need not to call [eventDetailedViewController release] . didSelectRowAtIndexPath in tableview...

EDIT:

I do have three views when I select it, it loads first nib. From first it loads second nib file. From second it loads third nib file. When I get back from third to second to first, my application crashes... I am thinking it's due to releasing my first view controller.

Apple's memory management rules are very simple: every call to alloc/new/copy*/retain must be balanced by a call to auto-/release. Anything (object or code block) that calls the former has ownership; when the owner no longer needs the owned object, release it. self.navigationController , for example, will retain a pushed view controller (asserting ownership) until the controller is popped form the navigation controller's stack. The only thing this simple(-istic?) explanation leaves out is when to use weak references (necessary to prevent retain cycles ).

Since eventDetailedViewController is a local variable, it's the code block that owns eventDetailedViewController . Since the variable isn't static, the variable will be discarded when the block exits and the block can't make use of the object after that, so the object needs to be released.

Since you're are not going to use eventDetailedViewController (you're setting it to nil), you should release it. The UINavigationController's pushViewController method will retain the object so it won't be released until the navigationController no longer needs it.

Setting it to nil, I believe, is only useful if it is a property that's nonatomic where setting it to nil will release the previous value. Since it's a local variable, you should release it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM