MKMapView Memory Leak in iPhone Application

后端 未结 3 1506
迷失自我
迷失自我 2021-01-14 09:14

I am working on an iPhone application which uses MKMapView and shows userlocation. I am getting memory leaks where leaked object is NSCFArray

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 09:29

    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 )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

提交回复
热议问题