Reputation: 2019
Are there any potential memory issues with the following code?:
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
- (void)viewDidUnload
{
[locationManager release];
locationManager=nil;
[super viewDidUnload];
}
I have checked it with Instrument and it says there is memory leaking with above code.
Upvotes: 3
Views: 713
Reputation: 2206
You should release the locationManager in the dealloc method.
- (void)dealloc
{
[locationManager release];
[super dealloc];
}
The reason for that is that viewDidUnload
is not guaranteed to get called.
For details see these questions:
When is UIViewController viewDidUnload called?
viewdidunload is not getting called at all!
Upvotes: 5
Reputation: 17877
It looks quite well besides:
viewDidLoad
add [super viewDidLoad];
.viewWillAppear:
add [super viewWillAppear:animated];
Upvotes: 0