App crashing when I add overlay to mkmapview

2019-04-30 03:22发布

I am trying to add annotations and overlays to a mapview but it crashes randomly. It is an EXC_BAD_ACCESS error, but zombies doesn't tell me anything. It says it is crashing on CG::Path::apply_transform(CGAffineTransform const&). I have looked everywhere for why this is happening but can't pinpoint it.

I am creating the mapview in ib and have the delegates and everything set up right. It will work sometimes and then crash randomly. I am using a gesture recognizer to add the annotations and overlay

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:2];
[self.mapView addGestureRecognizer:doubleTap];

and

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{

    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized){ 
        CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
        CLLocationCoordinate2D touchMapCoordinate = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];


        //add pin where user touched down...
        MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
        pa.coordinate = touchMapCoordinate;
        //[pa setTitle:@"title"];
        [mapView addAnnotation:pa];

        MKCircle* circle=[MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:500];
        [mapView addOverlay:circle];


    }

}

And the views for each:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        circleView.strokeColor = [UIColor redColor];
        circleView.lineWidth = 1.0;
        circleView.fillColor = [UIColor blackColor];
        circleView.alpha=.5;
        return circleView;

    }
    else
        return nil;

}


- (MKAnnotationView *)mapView:(MKMapView *)localmapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    if (![annotation isKindOfClass:[MKUserLocation class]]) {
        static NSString *AnnotationIdentifier = @"Annotation";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)[localmapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if (!pinView)
        {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
        }
        else
        {
                pinView.annotation = annotation;
            }
            return pinView;

        }
        return nil;
}

Is there a better way to add annotations/overlays to maps with user interaction? Am I doing something wrong in this code? It appears to draw most of the circle but then crashes... Is there some special trick to mapviews?

2条回答
在下西门庆
2楼-- · 2019-04-30 03:53

I am not sure where your EXC_BAD_ACCESS problems is. But you have a big problem with leaking memory. You have to releas object that you create with init. In the above code you create objects and never release them. That will not throw EXC_BAD_ACCESS but it will consume your memory.

Release the following object :

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];

MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
查看更多
聊天终结者
3楼-- · 2019-04-30 04:17

I've been getting the exact same error:

CG::Path::apply_transform(CGAffineTransform const&) will hit a test instruction and give me an EXC_BAD_ACCESS

This specifically occurs when using a double click on the map to zoom in on an MKCircle.

I can't say this definitively, but to the best of my knowledge this problem only occurs on the simulator when you use a double click to zoom, I've never been able to cause the error from an actual device, or by using option+click to zoom on the simulator.

So at this point I've filed this under "simulator bug" and left it at that.

If you do discover anything to the contrary, please let me know because it really bothers me not explicitly knowing whether or not this is a bug existing in my app that I just can't properly reproduce.

edit:

This was flagged initially as "not an answer" so I'll provide a little bit more information supporting my conjecture.

Basically in both of our scenarios a gesture is firing the re-rendering of the MKCircleView, what I strongly suspect is that since the simulator is able to generate some kind of gesture which can't be created from a user on the actual device, there is a failed expectation somewhere down the chain while that gesture gets handled.

查看更多
登录 后发表回答