内存警告-MapOverlay

时间:2012-11-13 17:08:49

标签: ios ios6 mkmapview

我对ios的内存使用有疑问。我已经实现了如下所示的代码,并在设备上运行大约10分钟,它停止并给我警告“收到内存警告”。我想知道为什么我会收到这个警告。

NSArray *ants = [mapView overlays];
for(bb = 0; bb < [polygonArray count]; bb++){
        int attr=[[idArray objectAtIndex:bb]floatValue];


        coords = malloc(sizeof(CLLocationCoordinate2D) * [[polygonArray objectAtIndex:bb] count]);
        for (int a = 0;a < [[polygonArray objectAtIndex:bb] count]; a++){
            coords[a].latitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:0]doubleValue];
            coords[a].longitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:1]doubleValue];
        }
        polygon = [[MKPolygon alloc]init];
        polygon = [MKPolygon polygonWithCoordinates:coords count:[[polygonArray objectAtIndex:bb]count]]; 
        //free(coords);
        [previousPolygons addObject:polygon];

            }
        [mapView addOverlay:polygon];
        }

    }
[mapView removeOverlays:ants];

1 个答案:

答案 0 :(得分:1)

你似乎有更多关闭括号,你有开放的那些,所以可能有比你给我们看的更多,但我已经展示了我认为将改善你的代码

NSArray *ants = [mapView overlays];
for(bb = 0; bb < [polygonArray count]; bb++){
    int attr=[[idArray objectAtIndex:bb]floatValue];


    coords = malloc(sizeof(CLLocationCoordinate2D) * [[polygonArray objectAtIndex:bb] count]);
    for (int a = 0;a < [[polygonArray objectAtIndex:bb] count]; a++){
        coords[a].latitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:0]doubleValue];
        coords[a].longitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:1]doubleValue];
    }
    //Remove the next line because polygonWIthCoordinates creates one for you
    //polygon = [[MKPolygon alloc]init];
    polygon = [MKPolygon polygonWithCoordinates:coords count:[[polygonArray objectAtIndex:bb]count]]; 
    //reinstate this line
    free(coords);
    [previousPolygons addObject:polygon];

}
//This is outside the for-loop so you'll only be adding the last polygon
[mapView addOverlay:polygon];
}

}
[mapView removeOverlays:ants];