iOS 6 MKMapView Memory Leak and Crashes app after some time

喜你入骨 提交于 2019-12-24 02:14:48

问题


iOS 6 MKMapView seems to be using the tons of memory, when we starts zooming and playing around with the Map View in our application(for around 7-10 mins), When we come out of the controller which has MKMapView, its some how not releasing the total memory(I am using ARC in my App). I am allocating and initializing the MKMapView thru NIB.

Controller 1 -->Controller 2 (has MKMapView)

1.5MB -->40-60MB (I have used the Instruments to find out leaks, but of no use)

When i come back to Controller1 the live bytes seems to be 7-10MB, after every visit to controller 2 there is around 2-3MB of increment in the Live Bytes, after some time it crashes the application, in console it says "Received Memory Warning". can any one please help? If you need any more info please let me know. Thanks in Advance.


回答1:


This is because of the way MKMapView works. There's an operation pending, so MapKit is retaining the MKMapView and it hasn't actually been deallocated yet. That isn't itself a problem. The problem is that it's still sending messages to your delegate.

The workaround is simple: As part of your view controller's cleanup set the map view's delegate to nil, which will prevent MKMapView from sending messages to it.

This is documented in MKMapViewDelegate Protocol Reference:

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.

Edit: Give Oscar an upvote as well, just below, who provided the documentation quote here.

Given ARC, I suggest this means you should set your map view's delegate to nil in your view controller's dealloc.

You can do some thing like this which resolves my issue . Change the type of map helps it too.

- (void)applyMapViewMemoryHotFix{

switch (self.mapView.mapView.mapType) {
    case MKMapTypeHybrid:
    {
        self.mapView.mapView.mapType = MKMapTypeStandard;
    }

        break;
    case MKMapTypeStandard:
    {
        self.mapView.mapView.mapType = MKMapTypeHybrid;
    }

        break;
    default:
        break;
}
self.mapView.showsUserLocation = NO;
self.mapView.delegate = nil;
[self.mapView removeFromSuperview];
self.mapView = nil;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [self applyMapViewMemoryHotFix];
}


来源:https://stackoverflow.com/questions/15922927/ios-6-mkmapview-memory-leak-and-crashes-app-after-some-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!