Templar
Templar

Reputation: 1694

MKAnnotationView memory leak issue

I needed recently to test my app with Allocations because of memory warnings. Even there are no leaks, the heap keeps growing with annotations added to the map. Every time I zoom in or out, the old annotations are removed, new ones are created and added to the map :

enter image description here

All of the memory locations from the NumberedAnnotationView group show the marked line as the problematic in viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    static NSString *reuseId_big   = @"bigcircle";     
    NumberedCircleAnnotationView * nca = nil;
    //nca = (NumberedCircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_big];  
    if ( nca == nil )
        nca = [[[NumberedCircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_big imageType:1] autorelease];  // THIS line
        nca.delegate = self;
    }
    return nca;
}

The init looks like this :

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageType:(int)imageType {
    
    self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];  // THIS line
    if (self != nil)
    {
     // set stuff 
    } 
    return self;
}

Even after minutes, these autoreleased objects are still there. ( 17 and 24 is the number of annotations displayed on map and removed with [mapView removeAnnotations:[mapView annotations]]; each time I zoom in/out.

The others, I think, are some MapKit generated stuff. I'm experiencing this in the simulator with versions 5.0 and 5.1.

How could I fix this ? Is something that I'm missing ? Or is this the normal behavior of Mapkit ?

Thanks!

Upvotes: 0

Views: 1238

Answers (1)

azamsharp
azamsharp

Reputation: 20068

Is there any reason you are not using the

[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

method?

Seems like you are creating view for your annotations every time!

Check out this article: http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx

Upvotes: 1

Related Questions