MKMapView 从 main.m 中的 autorealease 泄漏
我从这个论坛知道这是一个已知的错误,已报告给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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然作为 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 aCLLocationManager
, just use a singleCLLocationManager
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 thegetlocationgo
method.