0 Replies
      Latest reply on Mar 28, 2019 5:46 PM by tiletechnology
      tiletechnology Level 1 Level 1 (0 points)

        Ive created a navigation which pushes a view controller that contains an image view and a button which adds the same view controller each time the button is tapped. After each tap the memory grows and after each back tap the memory is not released despite deinit being called. There is nothing in the code that points to a memory leak is there something I am missing thank you?

         

        complete project repository

         

        class ViewController: UIViewController {
        
        
             lazy var nextButton:UIButton? = {
                let button = UIButton(type: .roundedRect)
                button.setTitle("Next", for: .normal)
                button.addTarget(self, action: #selector(nextButtonTapped), for: .touchUpInside)
                button.translatesAutoresizingMaskIntoConstraints = false
                button.backgroundColor = UIColor.red
                return button
            }()
        
             lazy var imageView:UIImageView? = {
                let image = #imageLiteral(resourceName: "DJI_0014")
                let imageView = UIImageView(image: image)
                imageView.translatesAutoresizingMaskIntoConstraints = false
                return imageView
            }()
        
            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
        
                guard let imageView = self.imageView, let nextButton = self.nextButton else{
                    print("imageView, nextButton are nil")
                    return
                }
        
                self.view.backgroundColor = UIColor.white
        
                self.view.addSubview(imageView)
                NSLayoutConstraint.activate([
                    imageView.topAnchor.constraint(equalTo:self.view.topAnchor),
                    imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
                    imageView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
                    imageView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)])
        
                view.addSubview(nextButton)
                NSLayoutConstraint.activate([nextButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
                    nextButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0),nextButton.heightAnchor.constraint(equalToConstant: 200),nextButton.widthAnchor.constraint(equalToConstant: 200)])
            }
        
            @objc func nextButtonTapped(){
                print("next button tapped")
                self.navigationController?.pushViewController(ViewController(), animated: true)
            }
        
            deinit {
                print("view controller is deinitialized")
            }
        
        }