CLLocationManager在发布时崩溃,但为什么呢?

Rog*_*Rog 4 iphone memory-management objective-c cllocationmanager ios

这可能是一个愚蠢的问题,一旦指出解决方案,让你觉得很愚蠢想知道你怎么没看到它但我无法弄清楚为什么我的应用程序的这部分与EXC_BAD_ACCESS崩溃(并且没有堆栈跟踪).

viewDidLoad如果启用了locationServices ,我将创建一个CLLocationManager*locationManager(在接口文件中声明的ivar):

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([CLLocationManager locationServicesEnabled])
    [self findUserLocation];
    ...
}

#pragma mark - Location finder methods

- (void)findUserLocation
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    [locationManager startUpdatingLocation];    
}
Run Code Online (Sandbox Code Playgroud)

所以位置管理器开始更新位置,每次都找到更新,调用下面的委托方法,在那里我检查是否应该超时或继续寻找我想要的准确度:

#pragma mark - CLLocationManager delegates

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp] > 8)
        [self locationManagerTimeOut];
    else if ((newLocation.horizontalAccuracy <= manager.desiredAccuracy) && (newLocation.verticalAccuracy <= manager.desiredAccuracy))
        [self locationManagerLockedPosition];
}
Run Code Online (Sandbox Code Playgroud)

如果某个位置被锁定,则调用此方法:

- (void)locationManagerLockedPosition
{
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    [locationManager release], locationManager = nil;
    NSLog (@"add results to view");
}
Run Code Online (Sandbox Code Playgroud)

如果它超时,这就是所谓的方法:

- (void)locationManagerTimeOut
{
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    [locationManager release], locationManager = nil;
    NSLog (@"Time out!");
}
Run Code Online (Sandbox Code Playgroud)

问题是,在任何一种情况下(超时或锁定位置),我在控制台中获得NSLog输出,然后2秒后应用程序崩溃?

有趣的是,如果我发表评论[locationManager release]...,一切正常但是为什么?此外,如果我移动[locationManager release]到我的dealloc方法,也没有崩溃!

我错过了一些基本的东西吗?

谢谢!

ROG

SVD*_*SVD 8

我有同样的问题,在CLLocationManager的深度可能存在一些问题.修复:

[locationManager stopUpdatingLocation];
[self performSelector:@selector(discardLocationManager) onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

并在discardLocationManager中执行:

- (void) discardLocationManager
{

  locationManager.delegate = nil;
  [locationManager release];

}
Run Code Online (Sandbox Code Playgroud)