I'm trying to get rid of a memory leak associated with an MKMapView. I think the main problem is that I created my entire project without using storyboard as a series of views which I manage by either setting the alpha to 0 or shrinking the view to a height of zero. I have a mapView initialized in ViewController.swift as such:
class ViewController: UIViewController{
//Properties
let mapView = MKMapView()
}
and then in another .swift file an extension of ViewController, something like:
extension ViewController {
private func setupMapViews(){
//MAPVIEW:
mapView.frame = CGRect(x: 0, y: view.frame.height, width: view.frame.width, height: 0)
mapView.overrideUserInterfaceStyle = .dark
mapView.mapType = .hybrid
mapView.delegate = self
}
On my app's main view I have a button which segues to this mapView using an animation. Checking the debug navigator as I run the simulator I see that the memory usage jumps from something like 90 MB to 160 MB when the segue occurs. When I press the Done button I have added to the mapView, the memory usage remains at around 160 MB, telling me there is a memory leak. I tried at first to simply remove it from the superview when the segue back to the main view controller occurs, but the app stays at around 160 MB, telling me that there is a retain cycle. I tried instead to change the declaration of the mapView to a weak var as such:
class ViewController: UIViewController{
//Properties
weak var mapView: MKMapView?
}
and then to initialize it in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
let mapV: MKMapView? = MKMapView()
self.mapView = mapV
}
but now when I attempt to segue to the mapView from my main view, it does not initialize, and the view does not appear. What am I doing wrong?
Any help is appreciated, thank you!