MKMapView从main.m中的autorealease泄漏

时间:2011-04-09 08:40:30

标签: iphone memory-leaks mkmapview instruments core-location

我从这个论坛知道这是已经报告给Apple的已知错误,但我担心每次调用视图时内存泄漏都会不断增加。 enter image description here

相关代码是

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

我已经在这几天前后回来了,任何帮助或提示都会很棒。 谢谢

编辑一个  尝试访问app delegate中的locationManager,一切都在运行,但IBaction的位置没有更新 这是IBaction中的代码,日志的结果是(null)

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

1 个答案:

答案 0 :(得分:0)

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