gpt4 book ai didi

iphone - 使用位置管理器发生内存泄漏

转载 作者:行者123 更新时间:2023-12-03 21:18:27 26 4
gpt4 key购买 nike

我将位置管理器与“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];

}

感谢大家的帮助!!!

最佳答案

您应该能够仅通过使用便捷方法来大幅减少代码(也使其更具可读性):

- (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 是自动释放的。

关于iphone - 使用位置管理器发生内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7660863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com