views:

437

answers:

1

I'm working on an iPhone app that shows a map with multiple circle overlays on certain locations. I'm running into serious memory issues and crashes when I add more than 6 circles and I zoom out far enough that they are all visible. When I zoom in such that only 2 circles are visible, all is fine. When I remove the MKOverlays, everything works fine.

Anyone who recognizes this behavior?

Code that creates the overlays. I store the overlays in a NSMutableDictionary for future reference (to be able to remove them from the map and to prevent double overlays)

- (void)updateMarkersForZones:(NSArray *)zones {
    NSLog(@"MapViewController: Update Markers");
    // For each zone, show a marker
    for (Zone* zone in zones) {
        NSString *keyMarker = [NSString stringWithFormat:@"%d-marker", zone.id];

        MKCircle *circle = [overlayCache objectForKey:keyMarker];
        if (circle == nil) {
            // draw the radius circle for the marker
            double radius = MAX(zone.markerRadius * 1.0, 1.0);
            circle = [MKCircle circleWithCenterCoordinate:zone.location radius:radius];
            [mapView addOverlay:circle];
            // store the circle in a cache for future reference
            [overlayCache setObject:circle forKey:keyMarker];
        }
    }
}

Code that makes the overlay views

#pragma mark -
#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
     MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:overlay] autorelease];
    circleView.lineWidth = 1.0;
    circleView.strokeColor = [UIColor redColor];
    return circleView;
}

Code that releases the overlay cache

- (void)dealloc {
    [overlayCache release];
    [mapView release];
    [super dealloc];
}
+2  A: 

I am seeing the same thing happen. I am drawing MKPolylines instead of circles, but I have exactly the same problem. 1 line works fine, but when I start to add several and try moving the map around it crashes with memory warnings. I'd paste my code, but it is pretty much identical to the above changing circle for line.

EDIT: the problem seems to be that each overlay creates a new core animation layer. there is a workaround here - https://devforums.apple.com/thread/48154?tstart=0 Also, I believe this is a known bug that should be fixed in the next release

clarky
Thanks, I'll try that work-around!
rule