Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)viewDidUnload {
    [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.

like image 367
fuzzygoat Avatar asked Apr 13 '26 09:04

fuzzygoat


1 Answers

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.

like image 103
deanWombourne Avatar answered Apr 15 '26 00:04

deanWombourne