应用程序崩溃与“调试终止”

时间:2010-10-18 07:19:02

标签: iphone

我有CLLocation变量声明我给它一个值,但当我在其他地方使用它时,应用程序崩溃“Debugging Terminated”没有任何登录控制台

CLLocation *userLoc;

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    userLoc=newLocation;
    [self somefunction];
}
-(void) somefunction
{
NSLog(@"%@",userLoc);
}

这里正确记录userLoc

但在我的另一种方法中

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*) indexPath
{
NSLog(@"%@",userLoc);
}

这里app崩溃了。 请帮忙

1 个答案:

答案 0 :(得分:4)

Core Location在委托方法中为您提供自动释放的CLLocation对象,因此在该方法之外变为无效。要保留它,您需要保留位置值:

userLoc=[newLocation retain];

或者,更好的是,使用retain属性为userLoc变量声明一个属性,并按以下方式使用它:

self.userLoc = newLocation;

P.S。 Memory management guide真是必读......