iOS MLeaksFinder检测内存泄漏

164 阅读1分钟

一开始报错,并且内存泄漏的弹框一直没弹出。

控制台报错如下:

Terminating app due to uncaught exception 'NSObjectNotAvailableException', reason: 'UIAlertView is deprecated and unavailable for UIScene based applications, please use UIAlertController!'

弹框如下:

代码如下:

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let firstVc = FirstViewController()
        self.navigationController?.pushViewController(firstVc, animated: true)
    }
import UIKit
import MLeaksFinder

class FirstViewController: UIViewController {

    var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.orange
        timer = Timer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        RunLoop.current.add(timer!, forMode: .common)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        timer?.invalidate()
        timer = nil
    }
    
    @objc func timerAction() {
        print("time。。。")
    }
    
    deinit {
        timer?.invalidate()
        timer = nil
    }
}

PS:添加deinit方法即可解决内存泄漏问题。好工具呀~~~