iPhone mapView / mapKit使用removeAnnotation& addAnnotation导致内存泄漏?

时间:2010-02-04 22:34:37

标签: iphone memory-leaks annotations mapkit android-mapview

在mapView上更新GPS指示器的位置...

[mapView removeAnnotation:myGpsAnnotation];
[myGpsAnnotation release];
myGpsAnnotation = nil;
myGpsAnnotation = [[MapLocationAnnotation alloc] initWithCoordinate:region.center annotationType:MapAnnotationTypeGps title:MAP_ANNOTATION_TYPE_GPS];
[mapView addAnnotation:myGpsAnnotation];

......我看到仪器(模拟器)中的网络记忆慢慢上升。没有“泄漏”闪烁,但“净字节”和“#Net”缓慢增加...除非此代码被注释掉。所以我100%肯定这是违法的代码。

但如果我做以下事情......

[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];

...然后“净字节数”和“#Net”增加得更快。这可能不是我的错误,而我正在尝试追踪MapKit中的泄漏?我真的在泄露记忆吗?再一次,“泄漏”下没有任何内容,但后来我不明白为什么净值会不断攀升。

感谢您的帮助,-Gord

3 个答案:

答案 0 :(得分:2)

您的发布周期错误:

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。您看到的内存增加可能来自annotationVIEWS。这些是由地图缓存的。如果你仍然看到内存增加可能你的观点dequeueing错误。

PPS。你是否考虑过设置注释的新位置?如果位置是唯一发生变化的话会容易得多。

myGpsAnnotation.coordinate = region.center;

答案 1 :(得分:1)

您应该首先了解收集的工作原理。

添加和反对收集将保留它。
从集合中删除对象将释放它。

在你的情况下,它是一个地图视图:


  1. 将注释添加到地图视图后,如果您拥有该引用,则应将其释放。
  2. 从地图视图中删除注释后,不需要将其释放。

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

    就是这样。无需在此发布。

答案 2 :(得分:0)

如果您在模拟器上进行测试时观察此信息,请不要担心。当在设备上运行模拟器时,地图工具包似乎缓存了内存中的地图图块,它使用SQLite存储地图图块而不是设备上有限的RAM。