我使用位置管理器“didUpdateToLocation”。我有一个内存泄漏,我无法解释:
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *) oldLocation {
[newLocation retain];
NSString *lCoordinates = [[NSString alloc] initWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];//Memory leak here!!!
[self setLocationCoordinates:lCoordinates];
[lCoordinates release];
NSString *lat = [[NSString alloc] initWithFormat:@"%f,%f", newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[lm stopUpdatingLocation];
NSMutableString *s = [[NSMutableString alloc]initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@&radius=10&sensor=true&key=---MyKey---", lat];
NSLog(s);
id delegate = self;
AsyncConnectionController * connectionController = [[[AsyncConnectionController alloc] initWithDelegate:delegate
selSucceeded:@selector(currentLocationConnectionSucceeded:)
selFailed:@selector(currentLocationconnectionFailed:)] autorelease];
NSURL *url = [[NSURL alloc] initWithString:s];
[connectionController startRequestForURL:url];
[lat release];
[s release];
[url release];
[newLocation release];
}
感谢大家的帮助!!!
答案 0 :(得分:1)
您应该能够通过使用便捷方法大幅减少代码(使其更具可读性):
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation {
NSString *lCoordinates = [NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[self setLocationCoordinates:lCoordinates];
NSString *lat = [NSString stringWithFormat:@"%f,%f", newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[lm stopUpdatingLocation];
NSString *s = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@&radius=10&sensor=true&key=---MyKey---", lat];
NSLog(s);
AsyncConnectionController * connectionController = [[[AsyncConnectionController alloc] initWithDelegate:self
selSucceeded:@selector(currentLocationConnectionSucceeded:)
selFailed:@selector(currentLocationconnectionFailed:)] autorelease];
NSURL *url = [NSURL urlWithString:s];
[connectionController startRequestForURL:url];
}
OP指定connectionController
是自动释放。