iPhone 和模拟器上的 EXC_BAD_ACCESS 问题

发布于 2024-10-31 14:51:38 字数 2263 浏览 5 评论 0原文

我在自己的代码之外得到了 EXC_BAD_ACCESS 。目前我的代码通过 shareURLCache 对象获取 URL,然后启动 url 连接。一旦我离开启动 url 连接的方法,我就会遇到 EXC_BAD_ACCESS。我尝试使用仪器来查找任何僵尸,并且分析了内存泄漏,但也没有出现。此时我完全陷入困境。

下面是加载 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");
    }
}

从返回 url 的共享 URLCache 中启动 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"];;
    }
}

I am getting an EXC_BAD_ACCESS out side of my own code. Currently my code gets a URL through a shareURLCache object and then starts url connection. Once I leave the method that starts the url connection I hit the EXC_BAD_ACCESS. I have tried using instruments to find any zombies and I have analysed for memory leaks and not turned up either. At this point I am completely stuck.

Here is the code that loads the url and starts the url connection

- (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");
    }
}

Method from the sharedURLCache that returns the 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"];;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-11-07 14:51:38

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

[location stopUpdatingLocation];

Just start by commenting out code and running to see if you can get the mem error to disappear then start adding code again until it appears, then you have a better idea where the issue is. it could be anywhere in your code since you seem to have a number of objects in there that are not passed in e.g. 'location'.

[location stopUpdatingLocation];
蘸点软妹酱 2024-11-07 14:51:38

我有同样的内存问题,

[mapView setRegion:region animated:YES];

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

I had same memory issue with

[mapView setRegion:region animated:YES];

This method doesn't like fast updates...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文