0

我在自己的代码之外得到了一个 EXC_BAD_ACCESS。目前,我的代码通过 shareURLCache 对象获取 URL,然后启动 url 连接。一旦我离开启动 url 连接的方法,我就会点击 EXC_BAD_ACCESS。我曾尝试使用仪器来查找任何僵尸,并且我已经分析了内存泄漏并且也没有出现。在这一点上,我完全被卡住了。

这是加载url并启动url连接的代码

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
       fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"At new location: %@",newLocation);

    MKCoordinateRegion region = 
            MKCoordinateRegionMakeWithDistance([newLocation coordinate], 750, 750);

    [mapView setRegion:region animated:YES];
    [location stopUpdatingLocation];

    CLLocationCoordinate2D coord = [newLocation coordinate];
    NSURL *url = [urlCache getReccomendationForUID:@"12345" atLat:coord.latitude 
                                         atLon:coord.longitude forCategories:nil];


    // Create the request.
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];


// create the connection with the request
// and start loading the data
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    if (connection) {
        // Create the NSMutableData to hold the received data.
        // xmlData is an instance variable declared elsewhere.
        xmlData = [[NSMutableData alloc]init];
    } else {
        NSLog(@"Could not create connection");
    }
}

sharedURLCache 中返回 url 的方法

-(NSURL *)getReccomendationForUID:(NSString *)u atLat:(double)lat atLon:(double)lon forCategories:(NSArray *)cat
{
    if(remote) {
        NSMutableString *categories = [[NSMutableString alloc]init];
        for(NSString *s in cat) {
            [categories appendString:@"&cat="];
            [categories appendString:s];
        }
        NSString *s = [NSString stringWithFormat:@"%@/recommendation?uid=%@&psw=null&lat=%f&lon=%f?%@",
                   apiRoot,u,lat,lon,categories];
        [categories release];
        return [NSURL URLWithString:s];
    } else {
        return [[NSBundle mainBundle] URLForResource:@"XMLTest" withExtension:@"xml"];;
    }
}
4

2 回答 2

0

只需先注释掉代码并运行以查看是否可以让内存错误消失,然后再次开始添加代码直到它出现,然后您就会更好地了解问题所在。它可能在您的代码中的任何位置,因为您似乎在那里有许多未传入的对象,例如“位置”。

[location stopUpdatingLocation];
于 2011-04-12T10:58:20.587 回答
0

我有同样的记忆问题

[mapView setRegion:region animated:YES];

这种方法不喜欢快速更新......

于 2012-01-19T22:20:18.483 回答