Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created April 24, 2018 10:10
Show Gist options
  • Save vialyx/53a03cb80c7d7dd4a60e167949fcef83 to your computer and use it in GitHub Desktop.
Save vialyx/53a03cb80c7d7dd4a60e167949fcef83 to your computer and use it in GitHub Desktop.
// MARK: - Strong reference cycle
class Owner {
let udid: String
let iin: String
let name: String
var business: Business!
init(udid: String, iin: String, name: String) {
self.udid = udid
self.iin = iin
self.name = name
}
deinit {
print("owner was deitialized")
}
}
class Business {
let title: String
var owner: Owner // <- place where we has been mistaken. It must be resoled using 'unowned' reference
init(title: String, owner: Owner) {
self.title = title
self.owner = owner
}
deinit {
print("business was deitialized")
}
}
var owner: Owner? = Owner(udid: "1st owner", iin: "1", name: "Mike")
owner!.business = Business(title: "1st Corp", owner: owner!)
/*
Do some work with owner
*/
owner = nil
/*
No outputs about deinitialization.
What happend?
Owner has strong reference to Business.
Business has strong reference to Owner.
!!! This is strong reference cycle. !!!
We have memory leak now!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment