1

i am currently facing a hard memory issue while using the new iOS7 MKPolygonRenderer class.. I pinpointed the source of the issue to a single line of code:

[renderer invalidatePath];

it seems like the core framework is not releasing the memory here, so that subsequent calls to this function result in an application crash due to memory exceptions.

basically what i want to do is to let the user alter a single polygon overlay on the map.

@interface MapViewController () <MKMapViewDelegate>
{
    MKPolygonRenderer* renderer;
}
@end

@implementation MapViewController

- (id) init
{
    if ((self=[super init]) != nil)
    {
        MKMapView* viewMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        viewMap.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        viewMap.delegate = self;
        [self.view addSubview:viewMap];
        viewMap.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(49.391759,8.675766), MKCoordinateSpanMake(0.35, 0.35));

        // a simple polygon overlay with 4 points
        CLLocationCoordinate2D overlayCoords[4] = { {49.421247,8.607101}, {49.418121,8.745117}, {49.321094,8.734818}, {49.320199,8.613968}/*, {49.370199,8.583968} */};
        MKPolygon* overlay = [MKPolygon polygonWithCoordinates:overlayCoords count:4];
        [viewMap addOverlay:overlay];

        // the gesture recognizer which is used to alter the polygon
        UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress_recognized:)];
        longPressRecognizer.minimumPressDuration = 0.1f;
        [viewMap addGestureRecognizer:longPressRecognizer];
    }
    return self;
}

- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    // reuse the renderer if already existent
    if (self->renderer == nil)
    {
        NSLog(@" renderer is nil ==> creating");
        renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
        renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4];
        renderer.strokeColor = [UIColor greenColor];
        renderer.lineWidth = 1;
   }
   else
       NSLog(@" renderer is not nil ==> reusing");
   return self->renderer;
}

- (void) longPress_recognized:(UILongPressGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        // begin drag
        // check if and if yes, which polygon point is touched
        // set indexOfSelectedPoint
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        // drag move

        if (indexOfSelectedMapPoint == -1)
            return;
        // get the coordinate of the user touch location
        CLLocationCoordinate2D c = [self->viewMap convertPoint:[sender locationInView:self->viewMap] toCoordinateFromView:self->viewMap];
        // update the coordinate of touched polygon point
        self->renderer.polygon.points[indexOfSelectedMapPoint] = MKMapPointForCoordinate(c);

        // this line causes the trouble 
        [renderer invalidatePath];
    }
    else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled)
    {
        // drag end
        // reset states
    }
}

my test project is using ARC. The profiler is not complaining about memory leaks.

is anybody having similar issues? am i doing something completely wrong here? is there a better way of doing this?

thanks for the help in advance

0 Answers0