MKMapView 从 main.m 中的 autorealease 泄漏
Posted
技术标签:
【中文标题】MKMapView 从 main.m 中的 autorealease 泄漏【英文标题】:MKMapView leaking from autorealease in main.m 【发布时间】:2011-04-09 08:40:30 【问题描述】:我从这个论坛知道这是一个已向 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]);
【问题讨论】:
【参考方案1】:虽然作为 Apple 问题,您将无法完全消除泄漏,但您绝对可以在每次触发 getlocationgo
时阻止它发生。与其不断创建CLLocationManager
,不如在应用程序的委托中使用一个CLLocationManager
(或创建一个单例来支持它)。这样,您在应用程序的生命周期中只分配/初始化一个位置管理器,而目前您每次重新加载该视图/调用 getlocationgo
方法时分配/初始化一个位置管理器。
【讨论】:
好点,我从应用代理访问 locationManager 没有任何成功 希望你能看看上面编辑中的代码以上是关于MKMapView 从 main.m 中的 autorealease 泄漏的主要内容,如果未能解决你的问题,请参考以下文章
在 Firebase 数据库中的 MKMapView 上放置多个标记
在启动时可靠地选择 MKMapView 中的注释(在 ios 6 和 ios 5.1 中)?