Giter Site home page Giter Site logo

retain-cycle-and-memory-leak's Introduction

Proved the Retain-cycle caused to Memory-leak in Closure and Delegate


Part1. Closure

To prove that using a closure will result in retain cycle, I created a simple model: a ViewController (VC1), and a custom DetailViewController class (VC2) will pass the information to the VC1 through the closure, and switch to another ViewController (VC3)

Also I want to confirm if the DetailViewController will disappear in the memory, because if it does not disappear, it means that VC1 and VC2 are strongly connected to each other, causing the memory leak.

To figure out the location of the variable in memory:

    var completionHandler: ((_ data: String) -> Void)? {
        didSet {
            print(self.completionHandler)
        }
    } 

If the DetailViewController disappear in the memory will print the string:

    deinit {
        print("----------------------")
        print("VC2 is bring removed from memory")
    }

pass the data to the VC1 through the closure:

    func requestData() {
        let data = "Data from VC2"
        completionHandler?(data)
    }

In VC1 I only need to do is use this closure and use the rootviewController to switch screens.

To Use the closure I just create, need to instance the DetailViewController :

	var detailVC: DetailViewController? = 
		DetailViewController()

Just print the data when we got it:

	detailVC?.completionHandler = { (data) in
            print("detailVC: \(self.detailVC!)")
            print(data)
        }

Change the RootView to VC3:

 	let storyboard = UIStoryboard(name: "Main", bundle: nil)
 	let viewController = storyboard.instantiateViewController(withIdentifier: "VC3")
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.window?.rootViewController = viewController

Although the data from the DataModel can be obtained normally, the VC2 has not disappeared and remains in the memory. It can be seen from the tool that Memory Leak has been caused.

image

image

image

The solution is to add [weak self] in VC1:

   detailVC?.completionHandler = { [weak self] (data) in
            print("detailVC: \(self.detailVC!)")
            print(data)
        }

Now if we use tool again, the memory leak error will gone, everything is good:

image


Part2. Delegate

To use the delegate, first define the protocol, then obey the method inside the protocol, and pass the data to VC1.

Define the protocol:

	protocol DataModelDelegate: class {
    	func didPassData(data: String)
}

    weak var delegate: DataModelDelegate? {
       didSet {
            print("Ya")
            print(self.delegate)
        }
    }

Use protocol pass data to VC1:

    let data = "Data from VC2"
       delegate?.didPassData(data: data)

Similarly, we observe whether vc2 disappears into the memory:

   deinit {
        print("----------------------")
        print("VC2 is bring removed from memory")
    }

Unfortunately the VC2 also has not disappeared and remains in the memory. It can be seen from the tool that Memory Leak has been caused:

image

image

image

The solution is to add weak keyword in VC2:

   weak var delegate: DataModelDelegate? {
        didSet {
            print("Ya")
            print(self.delegate)
        }
    }

Now if we use tool again, the memory leak error will gone, everything is good:

image

retain-cycle-and-memory-leak's People

Contributors

spockhsueh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.