2011-10-02 29 views
1

我遇到以下问题:在地图上使用json数据时发生内存泄露

我正在访问foursquare的Venue API并获取JSON字符串。我用this json-framework解析这个字符串。之后,我正在保存字典和阵列以访问有关场馆的更多信息(特别是我使用探索API)。所以场地信息在json结构树中被深入地保存(以我的经验)。在获得所需信息(地点名称&坐标)后,我在地图上放置了一个相应的管脚,地图上有相同的坐标,地点名称与管脚名称相同。

正好在我想要设置引脚名称的位置时,出现内存泄漏。所以在这里出了问题。如果我没有设置任何标题,所有工作都很好。所以只有当我将场地的名称设置为引脚时才会发生内存泄漏。

下面是相应的代码片段:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    //Parse JSON string 
    // Store incoming data into a string 
    NSString *jsonString = [[NSString alloc] initWithData:self.fetchedJSONData encoding:NSUTF8StringEncoding]; 
    [self.fetchedJSONData setLength:0]; 

    // Create a dictionary from the JSON string  
    NSDictionary *results = [NSDictionary dictionaryWithDictionary:[jsonString JSONValue]]; 
    [jsonString release]; 

    NSDictionary *response = [NSDictionary dictionaryWithDictionary:[results objectForKey:@"response"]]; 

    NSArray *groups = [NSArray arrayWithArray:[response objectForKey:@"groups"]]; 
    NSDictionary *groupsDic = [groups lastObject]; 
    NSArray *items = [NSArray arrayWithArray:[groupsDic objectForKey:@"items"]]; 

    for (int i=0; i<[items count]; i++) 
    { 
     CLLocationCoordinate2D annotationCoord; 
     MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; 
     NSDictionary* oneItemDoc = [NSDictionary dictionaryWithDictionary:[items objectAtIndex:i]]; 
     NSDictionary *singleVenue = [NSDictionary dictionaryWithDictionary:[oneItemDoc objectForKey:@"venue"]];    

     /* 
     *   Leak here in the next two lines! 
     * 
     */ 

     NSString *titleName = [[[singleVenue objectForKey:@"name"] copy] autorelease]; 
     annotationPoint.title = titleName; 

     NSDictionary *locationOfVenue = [NSDictionary dictionaryWithDictionary:[singleVenue objectForKey:@"location"]]; 

     annotationCoord.latitude = [[locationOfVenue objectForKey:@"lat"] doubleValue]; 
     annotationCoord.longitude = [[locationOfVenue objectForKey:@"lng"] doubleValue];    
     annotationPoint.coordinate = annotationCoord; 

     [self.mapView addAnnotation:annotationPoint]; 
     [self.annotationsArray addObject:annotationPoint]; 
     [annotationPoint release]; 
    } 
} 

当我要设置标题为annotationPoint因此发生泄漏。

对于每个场馆获取与JSON我得到以下泄漏痕迹(模糊库是我自己的库):

All blurred responsible libraries are my own app

有没有人建议如何解决这个问题呢?我尝试了很多很多东西。所以关键问题似乎是如何正确“交出”[singleVenue objectForKey:@"name"]。我第一次尝试设置它没有一个副本和一个autorelease,但后来我得到一个僵尸对象。所以我不知道该怎么做。我认为问题不是这两条线,而是一些线。我对吗?我也有建议,我的第三方JSON解析器强制这个问题(比较泄漏跟踪)。

所以我希望有人能帮我解决这个问题。会很棒!

更新:问题似乎与相应的JSON解析器无关。我用另一个解析器测试了我的代码,那里也存在同样的问题。所以它必须对我的代码本身做些事情。

我想我知道是什么问题。所以关闭地图后发生泄漏。所以在dealloc之后。所以可能是,我错过了那里的一些东西。我有一个mapview,我也释放它在dealloc中,并将其设置为无viewDidUnload。我也释放所有其他阵列等dealloc。有什么其他的(具体关于地图和视图),我需要释放?我认为这可能是问题!

更新:解决该问题:我不得不所有四方销标题和副标题设置为在dealloc方法零,因为一个值(通过JSON分析器访问)是由地图视图以某种方式保留。现在一切正常!

回答

1

解决该问题:我不得不所有四方销标题和副标题设置为在dealloc方法零,因为一个值(通过JSON分析器访问)是由地图视图以某种方式保留。现在一切正常!

0

有过类似的情况,注解的(MKPointAnnotation)标题没有正确释放。通过在发布之前将mapView设置为零来解决它。

我认为这比调用一个额外的[标题发布]更安全,它也可以工作,但是如果mapView在内部修复,会导致问题。