Search code examples
iosdebuggingswift5xcode11.4

Infinite Loop and Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeedaec488)


I'm getting a Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeedaec488) at the array for tabBar.viewControllers = [cameraVC, galleryVC] and in my console I'm getting my username endlessly printed out, which comes from a different class. The first code snippet shows where the thread was shown:

func addTabBar() {
    let cameraVC = HomeViewController()
    let galleryVC = GalleryViewController()

    let tabBarItem1 = UITabBarItem(title: "Camera", image: UIImage(named: "camera-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "blueCamera-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
    let tabBarItem2 = UITabBarItem(title: "Gallery", image: UIImage(named: "gallery-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "blueGallery-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))



    cameraVC.tabBarItem = tabBarItem1
    galleryVC.tabBarItem = tabBarItem2
    tabBarItem2.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -6, right: 0)

    tabBar.viewControllers = [cameraVC, galleryVC]

    self.view.addSubview(tabBar.view)
}

This is my second code snippet with all the functions defined where I'm getting my print statement for the username endlessly repeated in the console:

@IBAction func signUpTapped(_ sender: Any) {
    //validate the fields
    let error = validateFields()
    
    if error != nil {
        //something's wrong, show error message
        showError(error!)
    }
    else {
        
        //create trimmed data
        let userName = userNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        //create the user
        Auth.auth().createUser(withEmail: email, password: password) { (result,err) in
            
            //check for errors
            if err != nil {
                //error creating user
                self.showError(err?.localizedDescription ?? "Error creating user")
            }
            else {
                    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
                    changeRequest?.displayName = userName
                    print(userName)
                changeRequest?.commitChanges { (error) in print ("Username not created") }

                //transition to home screen
                self.saveLoggedState()
                self.transitionHome()
            }
        }
    }
}
func showError(_ message:String){
    
    errorLabel.text = message
    errorLabel.alpha = 1
}
func saveLoggedState() {
    
    let def = UserDefaults.standard
    def.set(true, forKey: "is_authenticated")
    def.synchronize()
    
}
func transitionHome() {
    
    let homeVC = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
    self.view.window?.rootViewController = UINavigationController(rootViewController: homeVC!)
    self.view.window?.makeKeyAndVisible()
    
}

I hope that's enough code! I'm really confused about what the problem is and previous stack overflow posts haven't been helpful. Any assistance would be much appreciated. Thanks!

Edit 1: I'm also implementing a side menu in my home controller. The code for it looks like this:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundColor = color
    view.backgroundColor = color
    tableView.tableFooterView = UIView()
}

// Table
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = menuItems[indexPath.row].rawValue
    cell.textLabel?.textColor = .white
    cell.backgroundColor = color
    cell.contentView.backgroundColor = color
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    // Relay to delegate about menu item selection
    let selectedItem = menuItems[indexPath.row]
    delegate?.didSelectMenuItem(named: selectedItem)
}

Solution

  • func addTabBar() {
       let cameraVC = HomeViewController()
       let galleryVC = GalleryViewController()
    }
    

    I guess it's a typo, you create HomeViewController inside a HomeViewController, which causes an infinite loop, and I think your Auth is called inside viewViewAppear/didLoad, so it triggers endlessly until you Stack overflow.

    If that's not the case, I guess your HomeViewController triggers signUpTapped, and then on user creation it goes back to HomeViewController (which triggers signUpTapped again), so a recursion without an end.