1

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

1
  • i'm interest to how you could do that, please if u have a peace of code could u paste it like we could learn also THX a lot for helping us...... Jan 22, 2011 at 14:37

3 Answers 3

2

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;
1

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:


  1. After adding the annotation to map view , you should release it if you own the reference.
  2. After removing an annotation from map view don't need ot release it.

 MyClass *obj=[[MClass alloc] init];
 [mapview addObject:obj];
 [obj release];
 ...
 [mapview removeAnnotation:obj];

That's it. No need to release here.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.