1
votes

I have the following simple code to track MKAnnotations in my MKMapView. I create the annotations in a loop through my list of locations I want to track. I add the annotations to my collection classes, an NSMutableDictionary and NSMutableArray, which are declared properties (not using ARC but am using Xcode 4.2 so using "strong" instead of retain since they are supposedly synonymous; for future compatibility with ARC).

If I call this routine a second time (say when the list of locations gets updated), the Leaks Instrument claims that the annotations are leaking, as well as the collection objects themselves. (I get a single NSMUtableDictionary in my leak list with the stack trace pointing to the line where the dictionary is created and set into my property, we well as a number of small leaks that match up to the annotations, and complaints about both the NSMutableArray and NSMutableDictionary). However, I don't see any violations of the memory management rules. I have a release paired with my annotation creation through alloc/init. I do an implicit retain through my property "setter" and when the collections are replaced, they should be released and then release all their content objects. If anyone can see where I go wrong, I would appreciate it.

@property(strong, nonatomic) NSMutableArray *annotationList;
@property(strong, nonatomic) NSMutableDictionary *annotationLocations; 

.

-(void) createMapAnnotations
{      
     if ([[self mapView] annotations])
        [[self mapView] removeAnnotations:[[self mapView] annotations]];

     [self setAnnotationList:[NSMutableArray array]];
     [self setAnnotationLocations:[NSMutableDictionary dictionary]];

     for (JEstablishmentLocation *loc in [[JLocationManager sharedLocationManager] localLocations])
        {
          MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
          [annotation setCoordinate:[[loc estCoordinates] coordinate]];

          [[self mapView] addAnnotation:annotation];
          [[self annotationList] addObject:[NSDictionary dictionaryWithObject:annotation forKey:[loc estKeyValue]]];
          [[self annotationLocations] setObject:loc forKey:[annotation description]];

          [annotation release];
          }
     [self centerOnCurrentLocation];
}
1
Have you tried clearing your array and dictionary before 'losing' them? Your code seems ok at first glance.jv42
Exchange strong with retain in spite of their semantic equivalence, does Instruments still show leaks? It is not an ARC code, you do release both ivars in dealloc, don't you?Tomasz Stanczak
I'll try the exchange of strong/retain. Yes the ivars are released in dealloc but the controller object does not die so that is never called.chadbag

1 Answers

0
votes

Instruments show where the leaked objects are allocated, it does not show where the leak is happening. I mean, maybe the object that is allocated on the pointed line, cause a leak somewhere else in your code.

I don't know if this may be the case for your code, though.