删除注释时 MKMapView 泄漏还是只有我泄漏?
我的注释被泄露了。有问题的代码(精简到最低限度)是:
- (void)updateLocations:(NSArray *)result {
[mapView removeAnnotations:locations];
[locations removeAllObjects];
[allLocations removeAllObjects];
for (NSDictionary *location in result) {
LocationAnnotation *annote = [[LocationAnnotation alloc] initWithInfo:location];
[allLocations addObject:annote];
[annote release];
}
[self updateAnnotations];
}
- (void)filterLocations {
for (LocationAnnotation *annote in allLocations) {
if (annote.typeFlag & filterFlags) {
[locations addObject:annote];
}
}
}
- (void)updateAnnotations {
[self filterLocations];
[mapView addAnnotations:locations];
}
- (void)updateFilter {
[mapView removeAnnotations:locations];
[locations removeAllObjects];
[self updateAnnotations];
}
allLocations
是一个包含所有注释的数组(它们不一定在地图上),locations
是保存地图中实际显示的位置的数组。当调用 updateLocations:
时,添加到地图中的部分(或全部,它会随着测试的不同而变化)注释会泄漏。注释的分配历史是:
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 LocationAnnotation Malloc 16421111296 1 0x4953870 64 MyApp -[MapViewController updateLocations:]
1 LocationAnnotation Retain 16421383424 2 0x4953870 0 MyApp -[MapViewController updateLocations:]
2 LocationAnnotation Release 16421391104 1 0x4953870 0 MyApp -[MapViewController updateLocations:]
3 LocationAnnotation Retain 16444210176 2 0x4953870 0 MyApp -[MapViewController filterLocations]
4 LocationAnnotation Retain 16557738240 3 0x4953870 0 MapKit -[MKQuadTrie insert:]
5 LocationAnnotation Retain 16557750272 4 0x4953870 0 MapKit -[MKAnnotationContainerView addAnnotation:]
6 LocationAnnotation Retain 16564529408 5 0x4953870 0 MapKit -[MKQuadTrie insert:]
7 LocationAnnotation Release 17296397312 4 0x4953870 0 MapKit -[MKAnnotationContainerView showAddedAnnotationsAnimated:]
8 LocationAnnotation Retain 21832317184 5 0x4953870 0 MapKit -[MKAnnotationContainerView removeAnnotation:]
9 LocationAnnotation Autorelease 21832324096 0x4953870 0 MapKit -[MKAnnotationContainerView removeAnnotation:]
10 LocationAnnotation Release 21832350208 4 0x4953870 0 MapKit -[MKQuadTrie remove:]
11 LocationAnnotation Release 21920062208 3 0x4953870 0 MyApp -[MapViewController updateLocations:]
12 LocationAnnotation Release 21923836416 2 0x4953870 0 MyApp -[MapViewController updateLocations:]
13 LocationAnnotation Release 22050286336 1 0x4953870 0 Foundation -[NSAutoreleasePool drain]
从这个来看,罪魁祸首似乎是 [MKQuadTrie insert:]
被调用了两次,而只调用了一次 [MKQuadTrie remove:]
。我是否遗漏了某些东西,这是我的错,还是 MKMapKit 中的错误?
编辑:我看到分配历史有 14 个 [MKQuadTrie insert:] 调用,只有 1 个 [MKQuadTrie remove:]
I'm getting leaks of my annotations. The code (stripped down to its bear minimum) in question is:
- (void)updateLocations:(NSArray *)result {
[mapView removeAnnotations:locations];
[locations removeAllObjects];
[allLocations removeAllObjects];
for (NSDictionary *location in result) {
LocationAnnotation *annote = [[LocationAnnotation alloc] initWithInfo:location];
[allLocations addObject:annote];
[annote release];
}
[self updateAnnotations];
}
- (void)filterLocations {
for (LocationAnnotation *annote in allLocations) {
if (annote.typeFlag & filterFlags) {
[locations addObject:annote];
}
}
}
- (void)updateAnnotations {
[self filterLocations];
[mapView addAnnotations:locations];
}
- (void)updateFilter {
[mapView removeAnnotations:locations];
[locations removeAllObjects];
[self updateAnnotations];
}
allLocations
is an array that contains all the annotations (they are not necessarily on the map), and locations
is an array that holds the locations that are actually displayed in the map. When updateLocations:
is called, some (or all, it changes from test to test) of the annotations added to the map are leaking. The allocation history of the annotations is:
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 LocationAnnotation Malloc 16421111296 1 0x4953870 64 MyApp -[MapViewController updateLocations:]
1 LocationAnnotation Retain 16421383424 2 0x4953870 0 MyApp -[MapViewController updateLocations:]
2 LocationAnnotation Release 16421391104 1 0x4953870 0 MyApp -[MapViewController updateLocations:]
3 LocationAnnotation Retain 16444210176 2 0x4953870 0 MyApp -[MapViewController filterLocations]
4 LocationAnnotation Retain 16557738240 3 0x4953870 0 MapKit -[MKQuadTrie insert:]
5 LocationAnnotation Retain 16557750272 4 0x4953870 0 MapKit -[MKAnnotationContainerView addAnnotation:]
6 LocationAnnotation Retain 16564529408 5 0x4953870 0 MapKit -[MKQuadTrie insert:]
7 LocationAnnotation Release 17296397312 4 0x4953870 0 MapKit -[MKAnnotationContainerView showAddedAnnotationsAnimated:]
8 LocationAnnotation Retain 21832317184 5 0x4953870 0 MapKit -[MKAnnotationContainerView removeAnnotation:]
9 LocationAnnotation Autorelease 21832324096 0x4953870 0 MapKit -[MKAnnotationContainerView removeAnnotation:]
10 LocationAnnotation Release 21832350208 4 0x4953870 0 MapKit -[MKQuadTrie remove:]
11 LocationAnnotation Release 21920062208 3 0x4953870 0 MyApp -[MapViewController updateLocations:]
12 LocationAnnotation Release 21923836416 2 0x4953870 0 MyApp -[MapViewController updateLocations:]
13 LocationAnnotation Release 22050286336 1 0x4953870 0 Foundation -[NSAutoreleasePool drain]
And looking at this it seems the culprit is that [MKQuadTrie insert:]
is called twice while only one [MKQuadTrie remove:]
is called. Am I missing something and its my fault or is it a bug in MKMapKit?
Edit: I have seen an allocation histroy with 14 [MKQuadTrie insert:] calls and only 1 [MKQuadTrie remove:]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我为我解决了这个问题
,我试图删除所有注释并添加新注释(但我的代码确保它不会删除用户位置)。这与该代码有关,
我用此代码替换了它
,现在我没有遇到同样的问题。
Ok i solved this problem for me
I was trying to remove all the annotations and add new ones (but my code was making sure it didn't remove the user location). It was something to do with that code
I replaced it with this
And now i don't get the same issue.