0

I am getting the error:

objc[12772]: Cannot form weak reference to instance (0x137d65240) of class appName.ViewController. It is possible that this object was over-released, or is in the process of deallocation.

I find this strange because this happened only after I have added a button click to bring the user to this UIViewController through instantiation. After running the app again, the error goes away so this occurs only when the user is segued into this UIViewController from a button.

Does anyone have any idea as to what causes this issue?

  @IBOutlet weak var time: UILabel!
    
    @IBOutlet private weak var startPause: UIButton! {
        didSet {
            startPause.setBackgroundColor(.green, for: .normal)
            startPause.setBackgroundColor(.yellow, for: .selected)
            startPause.setTitle("PAUSE".uppercased(), for: .selected)
        }
    }
    
    private lazy var stopwatch = Stopwatch(timeUpdated: { [weak self] timeInterval in // SIGNAL SIGABRT
        guard let strongSelf = self else { return }
        strongSelf.time.text = strongSelf.timeString(from: timeInterval)
    })
    
    deinit {
        stopwatch.stop()
    }
    
    @IBAction func toggle(_ sendler: UIButton) {
        sendler.isSelected = !sendler.isSelected
        stopwatch.toggle()
    }
    
    @IBAction func reset(_ sendler: UIButton) {
        stopwatch.stop()
        startPause.isSelected = false
    }
    
    private func timeString(from timeInterval: TimeInterval) -> String {
        let seconds = Int(timeInterval.truncatingRemainder(dividingBy: 60))
        let minutes = Int(timeInterval.truncatingRemainder(dividingBy: 60 * 60) / 60)
        let hours = Int(timeInterval / 3600)
        return String(format: "%.2d:%.2d:%.2d", hours, minutes, seconds)
    } 

Code To Present View Controller:

class TutorialViewController: UIViewController {
    
    @IBOutlet weak var doneTut: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    @IBAction func doneTut(_ sender: Any) {
       
       
        let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
        
        self.view.window?.rootViewController = homeViewController
        self.view.window?.makeKeyAndVisible()
        
    }
    
}
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
swiftnooby
  • 11
  • 2

1 Answers1

0

Ok, I think the way you are presenting the view controller is problematic. I would look into using UINavigationController, UITabController or your own container view controller with view controller containment to handle presentations: https://www.hackingwithswift.com/example-code/uikit/how-to-use-view-controller-containment

Basically, since you are resetting the window's root view controller, your presenting view controller, TutorialViewController, might be getting deallocated as a result (if nothing else is retaining it).

stackich
  • 3,607
  • 3
  • 17
  • 41
pietrorea
  • 841
  • 6
  • 14