阻止 iOS 7 MKMapView 泄漏内存 [英] Stop iOS 7 MKMapView from leaking memory
问题描述
我注意到我的应用程序会泄漏内存,但如果我将 MKMapView
取出,内存问题就会消失.
I've noticed that my app leaks memory, but if I take the MKMapView
out the memory problem goes away.
为了测试这个理论,我做了一个非常简单的项目,它有一个视图,它推送一个带有 MKMapView
的视图,然后弹出和推送.而已.视图控制器中没有代码,一切都通过故事板完成.
To test the theory, I made a dead simple project that has a view that pushes a view with a MKMapView
in it and pops and pushes. Nothing more. No code in the view controllers, everthing done via storyboard.
如果我来回切换到地图视图,它在推入和弹出包含地图的视图后开始大约 3MB,这大约是内存的 230MB 的 15 倍.
If I go back and forth to the map view, it starts about 3MB after doing pushing and popping the view with the map in it this about 15 times the memory is around 230MB.
还有人看到这个吗?看起来是一个相当大的错误.有没有不同的方法来使用 MKMapView
来防止它泄漏这么多?
Anyone else seen this? Seems like a pretty big bug. Is there a different way to use MKMapView
that will prevent it from leaking so much?
推荐答案
我遇到了同样的问题,并且(感谢 Stackoverflow)通过更改 viewWillDisappear
中的 MKMapType
解决了这个问题并将其委托解除分配/设置为 nil.因为它仍然向委托发送消息.这记录在 MKMapViewDelegate 协议参考:
I had faced the same issue and (thanks to Stackoverflow) fixed it by changing MKMapType
in viewWillDisappear
and deallocating/setting its delegate to nil.As it still sends message to delegates.
This is documented in MKMapViewDelegate Protocol Reference:
在发布您设置了一个 MKMapView 对象之前委托,请记住将该对象的委托属性设置为 nil.一您可以执行此操作的地方是在您处理的 dealloc 方法中地图视图
Before releasing an MKMapView object for which you have set a delegate, remember to set that object’s delegate property to nil. One place you can do this is in the dealloc method where you dispose of the map view
.
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self applyMapViewMemoryFix];
}
- (void)applyMapViewMemoryFix{
switch (self.mkMapView.mapType) {
case MKMapTypeHybrid:
{
self.mkMapView.mapType = MKMapTypeStandard;
}
break;
case MKMapTypeStandard:
{
self.mkMapView.mapType = MKMapTypeHybrid;
}
break;
default:
break;
}
self.mkMapView.showsUserLocation = NO;
self.mkMapView.delegate = nil;
[self.mkMapView removeFromSuperview];
self.mkMapView = nil;
}
希望能帮到你
这篇关于阻止 iOS 7 MKMapView 泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!