MKMapView 从 main.m 中的 autorealease 泄漏

发布于 2024-10-31 07:47:51 字数 3524 浏览 1 评论 0原文

我从这个论坛知道这是一个已知的错误,已报告给Apple,但我担心每次调用视图时内存泄漏都会不断增加。 在此处输入图像描述

相关代码是

-(IBAction)getlocationgo:(id) sender{
    //NSAutoreleasePool *pool;
    //pool = [[NSAutoreleasePool alloc] init];
    self.locationManager=[[[CLLocationManager alloc]init]autorelease];
    self.locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;


    //mapView.showsUserLocation =YES;
    //[pool release];


}

- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError*)anError
{
    switch([anError code])
    {
        case kCLErrorLocationUnknown: // location is currently unknown, but CL will keep trying
            break;

        case kCLErrorDenied: // CL access has been denied (eg, user declined location use)
            {UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Location Error"
                                  message:@"Please enable Location Services in the Settings menu"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
             AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

            [alert show];
            [alert release];}
            break;

        case kCLErrorNetwork: // general, network-related error
            {UIAlertView *alert = [[UIAlertView alloc]
                               initWithTitle:@"Location Error"
                               message:@"The Little Helper can't find you - please check your network connection or that you are not in airplane mode"
                               delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil];
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

            [alert show];
            [alert release];}
            break;
    }

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"thisruns");
    MKCoordinateSpan span;
    span.latitudeDelta =0.2;
    span.longitudeDelta =0.2;

    MKCoordinateRegion region;
    region.span = span;
    region.center = newLocation.coordinate;


    [mapView setRegion:region animated:YES];
    mapView.showsUserLocation =YES;
    mapView.mapType = MKMapTypeHybrid;

    latitude.text = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
    longitude.text = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    NSString *newloc=longitude.text;
    NSLog(@"long%f", newloc);

    [locationManager stopUpdatingLocation];

}

属性的属性与此相关

@property (nonatomic, retain) CLLocationManager *locationManager;

,并且已解除分配

mapView.delegate = nil;
[mapView release];
locationManager.delegate = nil;
[locationManager release];  

我一直在使用此来回已经好几天了,任何帮助或提示都会很棒。 谢谢

编辑一 尝试访问应用程序委托中的 locationManager,一切都会运行,但 IBaction 没有更新位置 这是 IBaction 中的代码,日志的结果是 (null)

 LLHelperAppDelegate *appDelegate = (LLHelperAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.locationManager startUpdatingLocation];
    appDelegate.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    NSLog(@"%@", [appDelegate locationManager]);

I know from this forum that this is a known bug that has been reported to Apple, but I am concerned that the memory leak keeps increasing everytime I call the view.
enter image description here

the relevant code is

-(IBAction)getlocationgo:(id) sender{
    //NSAutoreleasePool *pool;
    //pool = [[NSAutoreleasePool alloc] init];
    self.locationManager=[[[CLLocationManager alloc]init]autorelease];
    self.locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;


    //mapView.showsUserLocation =YES;
    //[pool release];


}

- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError*)anError
{
    switch([anError code])
    {
        case kCLErrorLocationUnknown: // location is currently unknown, but CL will keep trying
            break;

        case kCLErrorDenied: // CL access has been denied (eg, user declined location use)
            {UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Location Error"
                                  message:@"Please enable Location Services in the Settings menu"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
             AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

            [alert show];
            [alert release];}
            break;

        case kCLErrorNetwork: // general, network-related error
            {UIAlertView *alert = [[UIAlertView alloc]
                               initWithTitle:@"Location Error"
                               message:@"The Little Helper can't find you - please check your network connection or that you are not in airplane mode"
                               delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil];
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

            [alert show];
            [alert release];}
            break;
    }

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"thisruns");
    MKCoordinateSpan span;
    span.latitudeDelta =0.2;
    span.longitudeDelta =0.2;

    MKCoordinateRegion region;
    region.span = span;
    region.center = newLocation.coordinate;


    [mapView setRegion:region animated:YES];
    mapView.showsUserLocation =YES;
    mapView.mapType = MKMapTypeHybrid;

    latitude.text = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
    longitude.text = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    NSString *newloc=longitude.text;
    NSLog(@"long%f", newloc);

    [locationManager stopUpdatingLocation];

}

the property's are with this

@property (nonatomic, retain) CLLocationManager *locationManager;

and it is dealloced

mapView.delegate = nil;
[mapView release];
locationManager.delegate = nil;
[locationManager release];  

I have been going back and forward with this for a few days now, any help or tips would be great.
Thank you

Edit One
Trying to access locationManager in the app delegate, everything runs but there is no update to the location from the IBaction
This is the code in the IBaction and the result from the log is (null)

 LLHelperAppDelegate *appDelegate = (LLHelperAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.locationManager startUpdatingLocation];
    appDelegate.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    NSLog(@"%@", [appDelegate locationManager]);

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

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

发布评论

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

评论(1

可是我不能没有你 2024-11-07 07:47:51

虽然作为 Apple 问题,您无法完全消除泄漏,但每次触发 getlocationgo 时,您绝对可以阻止它发生。无需不断创建 CLLocationManager,只需在应用的委托中使用单个 CLLocationManager(或创建一个单例来支持它)即可。这样,您在应用的生命周期中只需分配/初始化一次位置管理器,而目前您每次重新加载该视图/调用 getlocationgo 方法时都会分配/初始化一个位置管理器。

Whilst as an Apple issue you won't be able to remove the leak entirely, you can definitely stop it from happening every time you trigger getlocationgo. Rather than constantly creating a CLLocationManager, just use a single CLLocationManager in your app's delegate (or create a singleton to support it). That way you'll only alloc/init a location manager once during your app's lifecycle, whereas currently you alloc/init one every time you reload that view / call the getlocationgo method.

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