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

发布于 2024-08-20 06:51:48 字数 1085 浏览 3 评论 0原文

要更新地图视图上 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 中的漏洞?我真的内存泄漏了吗?同样,“泄漏”下没有出现任何内容,但我不明白为什么净值会不断攀升。

感谢您的帮助,-戈德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-08-27 06:51:48

你的发布周期是错误的:

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

聚苯硫醚。您是否考虑过为注释设置新位置?如果位置是唯一改变的事情,那就容易多了。

myGpsAnnotation.coordinate = region.center;

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;
一身软味 2024-08-27 06:51:48

您应该首先了解集合是如何工作的。

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

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


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

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

就是这样。这里不需要释放。

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.

狼亦尘 2024-08-27 06:51:48

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文