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

发布于 2024-12-08 02:45:03 字数 1488 浏览 0 评论 0原文

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

}

感谢大家的帮助!!!

I use the location manager with "didUpdateToLocation". I have a memory leak that I cannot explain:

- (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];

}

Thanks all for your help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-12-15 02:45:03

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

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

You should be able to reduce your code substantially just by using convenience methods (makes it more readable as well):

- (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 specifies that connectionController is autorelease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文