1

在 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 Bytes”和“#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 Bytes”和“#Net”增长得更快。这可能不是我的错误,我正在尝试追踪 MapKit 中的泄漏吗?我真的在泄漏内存吗?同样,“泄漏”下什么也没有出现,但我不明白为什么净值会不断攀升。

感谢您的帮助,-戈德

4

3 回答 3

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。这些由地图缓存。如果您仍然看到 mem 增加,则您的视图出列可能错误的。

聚苯乙烯。您是否考虑过为注释设置新位置。如果位置是唯一改变的东西,那就容易多了。

myGpsAnnotation.coordinate = region.center;
于 2011-03-10T10:55:32.780 回答
1

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

向集合添加和对象将保留它。
从集合中移除一个对象将释放它。

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


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

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

而已。无需在这里发布。

于 2011-03-04T07:38:05.460 回答
0

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

于 2010-02-13T17:29:55.737 回答