Reputation: 23
To update the location of a GPS indicator on mapView...
[mapView removeAnnotation:myGpsAnnotation];
[myGpsAnnotation release];
myGpsAnnotation = nil;
myGpsAnnotation = [[MapLocationAnnotation alloc] initWithCoordinate:region.center annotationType:MapAnnotationTypeGps title:MAP_ANNOTATION_TYPE_GPS];
[mapView addAnnotation:myGpsAnnotation];
...I see net memory slowly climbing in Instruments (simulator). No "Leak" blip, but "Net Bytes" and "# Net" slowly incrementing... unless this code is commented out. So I'm 100% certain this is the offending code.
BUT if I do the following...
[mapView removeAnnotation:myGpsAnnotation];
[myGpsAnnotation release];
myGpsAnnotation = nil;
myGpsAnnotation = [[MapLocationAnnotation alloc] initWithCoordinate:region.center annotationType:MapAnnotationTypeGps title:MAP_ANNOTATION_TYPE_GPS];
[mapView addAnnotation:myGpsAnnotation];
[mapView removeAnnotation:myGpsAnnotation];
[mapView addAnnotation:myGpsAnnotation];
[mapView removeAnnotation:myGpsAnnotation];
[mapView addAnnotation:myGpsAnnotation];
...then the "Net Bytes" and "# Net" increase much faster. Is it possible this isn't my mistake, and I'm trying to track down a leak in MapKit? Am I really leaking memory? Again, nothing appears under "Leaks", but then I don't see why Net values would be continually climbing.
Thanks for any help, -Gord
Upvotes: 1
Views: 5894
Reputation: 16029
Your release cycle is wrong:
myGpsAnnotation = [[MapLocationAnnotation alloc] initWithCoordinate:region.center annotationType:MapAnnotationTypeGps title:MAP_ANNOTATION_TYPE_GPS];
//retain count = 1
[mapView addAnnotation:myGpsAnnotation];
//retain count = 2 (the map does an extra retain)
[myGpsAnnotation release];
//retain count = 1
myGpsAnnotation = nil; //not really necessary
[mapView removeAnnotation:myGpsAnnotation];
//retain count = 0 -> dump (you can do this on the original place; I put it here to show the cycle)
PS. the memory increase you see is probably from the annotationVIEWS. These are cached by the map. If you still see increase in mem probably your view dequeueing is wrong.
PPS. did you consider just setting the new location for the annotation. Much easier if the location is the only thing that changes.
myGpsAnnotation.coordinate = region.center;
Upvotes: 2
Reputation: 1072
You should first understand how collection works.
Adding and object to collection will retain it.
Removing an object from collection will release it.
In your case it's a map view:
MyClass *obj=[[MClass alloc] init];
[mapview addObject:obj];
[obj release];
...
[mapview removeAnnotation:obj];
That's it. No need to release here.
Upvotes: 1
Reputation: 1
If you are observing this while testing on the simulator, don't worry. It seems that the map kit caches map tiles in memory when running on the simulator while on the device, it uses SQLite for storing map tiles and not the limited RAM on the device.
Upvotes: 0