【问题标题】:Application get crash while using NSAutoreleasepool inside MKMapview regionDidChangeAnimated method在 MKMapview regionDidChangeAnimated 方法中使用 NSAutoreleasepool 时应用程序崩溃
【发布时间】:2010-06-12 11:06:41
【问题描述】:

我正在开发一个地图应用程序,因为我喜欢在用户更改地图视图时放下图钉(如在 Zillow 应用程序中)。我正在使用以下代码。我正在尝试使用 NSAutoreleasepool 从服务器加载 xml 数据,以在后台线程中进行 xml 解析。

  • (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    NSLog(@"inside region did changed ");

    urlString =[NSString stringWithFormat: @"http://asdfasdasdf.com/asdfasdf/mapxml.php];
    
    [stories1 release];
    
    [mapview removeAnnotations:eventPoints1];
    
    eventPoints1 = [[NSMutableArray array] retain];
    
    [self performSelectorInBackground:@selector(callParsing) withObject:nil];
    

}

-(void)callParsing{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self parseXMLFileAtURL:urlString];

[self performSelectorOnMainThread:@selector(droppingPin) withObject:nil waitUntilDone:YES];

[pool drain];

}

上面的代码工作正常,但是一旦我更改了地图视图,应用程序就会崩溃。谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: iphone objective-c nsautoreleasepool


    【解决方案1】:

    从 stringWithFormat 返回时,urlString 已经自动释放。 由于您在 callParsing 中使用了在不同线程上执行的 urlString,因此您应该将其作为对象传递给该方法。否则,您可能会在 callParsing 方法执行之前将其释放,从而导致崩溃:

    ...
    [self performSelectorInBackground:@selector(callParsing:) withObject:urlString];
    ...
    
    -(void)callParsing:(NSString*)urlString {
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2011-12-25
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多