开发者

Memory management & viewDidUnload?

If I have a viewController setup as below:

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
}

when it comes to memory management should I be adding release to both viewDidUnload & dealloc?

-(void)viewDidUnloa开发者_开发知识库d {
    [locationManager release];
    locationManager = nil;
    [super viewDidUnload];
}


-(void)dealloc {
    [locationManager release];
    [super dealloc];
}

cheers Gary

EDIT:

[super dealloc] moved to bottom as per Deans kind comment.


Short answer :

Unless you are creating/retaining it in viewDidLoad (or a xib), don't release it in viewDidUnload.

Long answer :

viewDidUnload is used to release anything that you might have made when the view is created - this included things in viewDidLoad but also includes and IBOutlet properties that are created from inside a xib file. these should all be released and set to nil in viewDidUnload.

Anything else should just be released in dealloc.

The idea is that if viewDidUnload is called to free some memory, the view can be recreated again completely from your viewDidLoad method.


In viewDidUnload you should be setting your IBOutlet properties to nil and anything that is initialized in viewDidLoad.

Remember that if the phone is low on memory, your view will be unloaded if it isn't on-screen. Next time your view is loaded again, new views will be connected to the IBOutlets and viewDidLoad will be called again. So you should set the outlet properties to nil in viewDidUnload to reduce the memory footprint.


The guy is doing a [object release] before doing the self.object = nil;

Is the first release for nothing ? In the Apple documentation, they simply affect nil to the variable... what is right?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜