MKMapView does not free memory for MKUserLocation

I have a navigation controller in which VC1 pushes VC2 into the navigation stack. VC2 has MKMapView in a tab-based view with the user's location enabled. When I check re-selection with tools using the Heapshot Analysis tool, I repeatedly find that some MKUserLocation objects are not freed when I return to VC1. enter image description here

I deleted all annotations and also disabled the user's location on dealloc. What could be causing the heap to grow?

The VC1 code that pushes VC2 onto the navigation stack:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { VC2 *vc2 = [[VC2 alloc] init]; vc2.index = indexPath.row; [[NSNotificationCenter defaultCenter] removeObserver:self]; [[self navigationController] pushViewController:vc2 animated:YES]; [vc2 release]; vc2 = nil; return nil; } 

Dealloc code in VC2:

 - (void)dealloc { //Other UILabel, UITextField objects dealloc methods go here //The next four lines of code do not make a difference even if they are in viewWillDisappear [self.mapView removeAnnotations:self.mapView.annotations]; [self.mapView.layer removeAllAnimations]; self.mapView.showsUserLocation = NO;//This line does not make a difference in heapshot mapView.delegate = nil; [mapView release]; mapView = nil; [super dealloc]; 

}

Also, there is no heap growth unless I include the user's location.

Update: I tested this on a simulator and on an iPad 3G + WiFi, and I find this heap growth in both cases.

+4
source share
1 answer

If you create MKMapView in IB, you must release / nil -in its -viewDidUnload AND -dealloc .

If you do not, if your view is unloaded / reloaded for the duration of your view controller, all of your view elements (which are set up as saved properties in any case) will leak on reboot.

There seems to be a decent amount of chatter on the / SO network that says this is an API bug prior to 5.0.1. What version of the SDK are you building against?

Also filmed in the dark: try only

 self.mapView.showsUserLocation = NO; 

or

 self.mapView.showsUserLocation = NO; [self.mapView.layer removeAllAnimations]; [self.mapView removeAnnotations:self.mapView.annotations]; 

instead of the order of these commands that you are using now.

Maybe you delete the annotation for MKUserLocation before MKMapView has the ability to tear it correctly?

+5
source

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


All Articles