MKMapView Memory Leak in iPhone Application

2020-03-26 01:52发布

I am working on an iPhone application which uses MKMapView and shows userlocation. I am getting memory leaks where leaked object is NSCFArray of size 128 Bytes, GeneralBlock-16, GenralBlock-8 when is set MKMapView's showUserLocation property as TRUE. If is set it as NO then i dont get this leak. Can anyone suggest that what can be the possible reason for this. Is this a bug in MKMapView class or is am I using the MKMapView incorrectly. Can someone tell me what is the best way to use MKMapView and show userLocation also.

Thanks & Regards, Priyanka Aggarwal

3条回答
聊天终结者
2楼-- · 2020-03-26 02:00

I fixed a similar issue by autoreleasing my annotationView objects. Also, MKUserLocation is an annotation object, so checking for your own annotation objects (or checking to see if the annotation object is MKUserLocation), and returning nil for other annotation objects (or MKUserLocation) will tell map kit to use the default MKUserLocation object. Putting these checks into place could stop your leak. See below:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{ static NSString *placemarkIdentifier = @"placemark_identifier";
    if ([annotation isKindOfClass:[MyPlaceMark class]]) {
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
        if (annotationView == nil) {
            annotationView = [[[MyPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier] autorelease];
        } else {
            annotationView.annotation = annotation;
        }
        return annotationView;
    }
    return nil;
}

MKUserLocation class reference

查看更多
我命由我不由天
4楼-- · 2020-03-26 02:12

I have the exact same issue. It looks like a bug in MKMapView to me. Three workarounds come to mind:

  1. Create your own annotation for the current position.
  2. Don't destroy and recreate the view so you only get the leak once.
  3. Turn it off.

Fortunately for me I can turn it off without any significant loss of feature.

查看更多
登录 后发表回答