MapKit iOS8 memory leak

I read other StackOverflow questions and answers, and understand that this is a bug with iOS6 (or by design, release the delegate if necessary, and then see who knows). I do not know why and how it was not fixed.

Anywho, I added hot fixes from other answers (below, for future readers):

- (void) viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self applyMapViewMemoryHotFixOnDisappear]; } - (void)applyMapViewMemoryHotFixOnDisappear{ [self applyMapViewMemoryHotFix]; self.mapView.showsUserLocation = NO; self.mapView.delegate = nil; self.locationManager.delegate = nil; [self.mapView removeFromSuperview]; self.mapView = nil; } - (void)applyMapViewMemoryHotFix{ switch (self.mapView.mapType) { case MKMapTypeHybrid: { self.mapView.mapType = MKMapTypeStandard; } break; case MKMapTypeStandard: { self.mapView.mapType = MKMapTypeHybrid; } break; default: break; } self.mapView.mapType = MKMapTypeStandard; } -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { [self applyMapViewMemoryHotFix]; } 

However, I have a question: why doesn't the memory drop to the MapKit level?

Memory Usage Before / After MapKit

Is there anything else I am missing? Is this expected behavior? There are no memory leaks, judging by the profiler, but obviously something is wrong ...

+5
source share
1 answer

Despite using the much-loved MemoryHotFix community SO to deal with this issue, you should be sure that you don't have a strong link. As others have said, if you use (read instances) views that contain a link to the view controller in which they are located and the same view controller as the link to this view, you can get into a strong link loop.

Such a situation may block your deinit / dealloc methods, which make your cleanup unnecessary and useless.

As stated in the documentation :

You allow strong reference loops by defining some relationships between classes as weak or unpublished links, and not as strong links.

Therefore, do not forget:

  • Check if your deinit (swift) / dealloc is actually called (if not, it could indicate a strong reference loop).
  • In fact, nil delegates are MKMapView and LocalitionManager, as well as themselves.

Like this:

 self.mapView.delegate = nil self.mapView = nil self.locationManager?.delegate = nil self.locationManager = nil 

Proof

With that in mind, here is an example where one VC clicks another VC with MKMapView, each vertical red line means “clicking a new VC”, and each green line means “popping it”:

enter image description here

There is an initial setup (starting at 50 MB +/-), but a future push does not cause a memory leak, as shown. It is worth noting that this was recorded using a real device. I also tested it using a simulator, and the results were coherent, even though the initial setup was much higher (starting at 100 MB).

Hope you guys can fix this and you can also check your project for Strong link cycles that could harm your product in the future;)

+1
source

Source: https://habr.com/ru/post/1205079/


All Articles