iPhone mapView / mapKit 使用removeAnnotation & addAnnotation 导致内存泄漏?
要更新地图视图上 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的发布周期是错误的:
PS。您看到的内存增加可能来自注释视图。这些由地图缓存。如果您仍然看到内存增加,可能是您的观点 出队是错误的。
聚苯硫醚。您是否考虑过为注释设置新位置?如果位置是唯一改变的事情,那就容易多了。
Your release cycle is wrong:
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.
您应该首先了解集合是如何工作的。
将对象添加到集合中将保留它。
从集合中删除对象将释放它。
在您的情况下,它是一个地图视图:
就是这样。这里不需要释放。
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:
That's it. No need to release here.
如果您在模拟器上测试时观察到这一点,请不要担心。当在模拟器上运行时,地图套件似乎会在内存中缓存地图图块,而在设备上运行时,它使用 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.