Search code examples
iosswiftmfmailcomposeviewcontroller

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior


I am trying to implement a send mail code but I'm getting the following error :

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Here is my code piece:

    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["@gmail.com"])            
        presentViewController(mail, animated: true, completion: nil)
    } else {
        // show failure alert
    }

Solution

  • The error that you received could happen if you forgot to remove the segue to another function of your button.