1

我相信我已经阅读并遵循了大多数关于如何为用户接收位置更新的教程。我有一个类(UserTracker),它导入 CoreLocation 并实现正确的委托协议。我也实施了正确的方法。问题是当我用这种方法测试类时:

-(bool)test
{
    UserTracker * ut = [[UserTracker alloc] init];
    [ut startTracking]; 

    [NSThread sleepForTimeInterval:30];

    [ut stopTracking];

    NSLog(@"Accumulated distance is %f m", [pt sumDist]);

    return true;
}

我收到以下错误:

libdispatch.dylib`dispatch_semaphore_signal:
0x1e07e02:  movl   4(%esp), %eax
0x1e07e06:  movl   $1, %ecx
0x1e07e0b:  lock                         Thread 1: EXC_BAD_ACCESS (code=2, address=0x20)
0x1e07e0c:  xaddl  %ecx, 32(%eax)
0x1e07e10:  incl   %ecx
0x1e07e11:  testl  %ecx, %ecx
0x1e07e13:  jg     0x1e07e22                 ; dispatch_semaphore_signal + 32
0x1e07e15:  cmpl   $2147483648, %ecx
0x1e07e1b:  je     0x1e07e25                 ; dispatch_semaphore_signal + 35
0x1e07e1d:  jmp    0x1e081c1                 ; _dispatch_semaphore_signal_slow
0x1e07e22:  xorl   %eax, %eax
0x1e07e24:  ret    
0x1e07e25:  ud2    

顺便说一句,两者都在同一个线程上运行(这是一个问题吗?)。

UserTrack.m 中的一些方法:

-(void)startTracking
{
    if(_isTracking)
        return;
    // Clear old data
    [_locations removeAllObjects];
    [_lm startUpdatingLocation];
    _isTracking = true;
    NSLog(@"is tracking!");
}

// Stop tracking GPS coordinates
-(void)stopTracking
{
    if (!_isTracking)
        return;
    if(_lm != nil)
        [_lm stopUpdatingLocation];
    _isTracking = false;
    NSLog(@"STOPPED TRACKING");
}

-(id)init
{
    if(self = [super init])
    {
        _isTracking = false;
        _locations = [[NSMutableArray alloc] init];
        _lm = [[CLLocationManager alloc] init];
        _lm.delegate = self;
        _lm.distanceFilter = kCLDistanceFilterNone; // whenever we move
        _lm.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    }

    return self;
}

也在同一个类中实现:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
// Callback: new GPS coordinate available
-(void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation

没有被调用...这是线程问题吗?为什么我会收到这个奇怪的信号量错误?

4

1 回答 1

0

位置管理器类在这里进行了测试:

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        // TODO: remove this class
        BPTest * bt = [[BPTest alloc] init];
        [bt runTests];
        // Above must be removed and the location test inserted directly here...

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([BPAppDelegate class]));    
    }
}

作为一种快速的“hacky”方式来测试它......只有当我试图将测试完全移出“BPTest”类并在调用“UIApplicationMain”之前直接执行它时,才似乎得到了委托方法叫...

于 2012-10-05T13:17:45.867 回答