Khaled Alanezi
Khaled Alanezi

Reputation: 361

Displaying MapKit for second time fails with EXC_BAD_ACCESS

I've a problem but lots of googling and SO posts did not help. In my iOS swift app, I have a tab bar controller with two tabs. When the user clicks on the second tab, the app switches to a second screen for creating an event. The user can switch between the two tabs freely. While on the second tab for creating an event, the user can add multiple candidate locations for the event. The way the user is able to add the locations should be through a map and I use MapKit. I use a push segue to send the user to a new UIViewController with a map inside it. When the user clicks the add location button, the map is shown properly (currently I didn't do any logic for interacting with the map). My problem is that, if the user clicks the back button (to dismiss the map screen) and try again to add location, the UIViewController will display a gray map and the app will hang at the line:

class AppDelegate: UIResponder, UIApplicationDelegate {

and the error message is :

 Thread 1: EXC_BAD_ACCESS (code=1, address=0x103c)
 #0 0x00699440 in EAGLContext_renderbufferStorageFromDrawable(EAGLContext*, objc_selector*, unsigned int, id<EAGLDrawable>) ()

I know that many SO posts talked about memory issues such as this SO post and I did try their solution but it didn't help.

This is the very simple code for my UIViewController that shows the map:

import UIKit
import MapKit
import ObjectMapper
import CoreLocation


class AddRestaurantViewController: UIViewController{


@IBOutlet weak var theMapView: MKMapView!


override func viewDidLoad() {
    super.viewDidLoad();
    println("viewDidLoad")

}

override func viewWillAppear(animated: Bool) {
    println("viewWillAppear")
}

override func viewWillDisappear(animated: Bool) {
    println("viewWillDisappear")
}

override func viewDidDisappear(animated: Bool) {
    self.purgeMapMemory()
}

func purgeMapMemory(){
    println("Now purging...")
    self.theMapView.mapType = MKMapType.Standard
    self.theMapView.removeFromSuperview()
    self.theMapView = nil
}

}

Any hint?

Upvotes: 1

Views: 390

Answers (1)

Jakehao
Jakehao

Reputation: 1019

This problem has wasted a day of my life. After a huge time on testing, I conclude that maybe it is a bug for iOS 8.3 on iPhone 6 plus.

So to sum up,

My app crashes when,

1) I am debugging in Xcode on my iPhone 6 plus;

2) I make up an empty UIViewController (RootVC) that contains a UIButton, whose action is pushing to another UIViewController (MapVC) that only has a MKMapView in it;

3) So when I tap the button and push to MapVC, then I return to RootVC by tapping the back button on navigationBar. Then again when I tap the button again to push into MapVC, the app crashes.

However, when I am not running the app from Xcode, the app doesn't crash on the same device. For example, when I upload my app to Testflight and download the app on my iPhone 6 plus, it doesn't crash.

Also, I have tested on iPhone 5, iphone 6 with iOS 8.4 on it. The app doesn't crash.

And I can not reproduce the problem by creating a new project and do the same thing, crashes only happen in my existed project.

So I have to update my iPhone 6 plus to iOS 8.4, then this problem gone. Since it only happens when debugging from Xcode, I have to take the risk to believe that it will not crash after user download the app from app store.

Upvotes: 2

Related Questions