0

我对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];
4

1 回答 1

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];
于 2012-11-13T20:32:54.507 回答