Created
September 1, 2016 10:02
-
-
Save marcorei/85a10a1b107f5aed29caf562b14f3d6f to your computer and use it in GitHub Desktop.
Test if closures alone create reference cycles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Alf { | |
static var numberOfInstances = 0 | |
init() { | |
Alf.numberOfInstances += 1 | |
print("Alf instances: \(Alf.numberOfInstances)") | |
} | |
deinit { | |
Alf.numberOfInstances -= 1 | |
print("Alf instance: \(Alf.numberOfInstances)") | |
} | |
} | |
class Bernd { | |
static var numberOfInstances = 0 | |
let grillen:String | |
init(grillen:String = "gepflegt") { | |
self.grillen = grillen | |
Bernd.numberOfInstances += 1 | |
print("Bernd instances: \(Bernd.numberOfInstances)") | |
} | |
deinit { | |
Bernd.numberOfInstances -= 1 | |
print("Bernd instance: \(Bernd.numberOfInstances)") | |
} | |
} | |
var closure: (() -> (() -> ()))? | |
closure = { | |
_ in | |
var bernd = Bernd() | |
var innerClosure : () -> () = { | |
_ in | |
print(bernd.grillen) | |
var alf = Alf() | |
} | |
bernd = Bernd(grillen: "ungepflegt") | |
return innerClosure | |
} | |
var secondClosure: (()->())? = closure!() | |
secondClosure!() | |
secondClosure!() | |
secondClosure = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prints:
So everything should get
collected by GCdeallocated, ARC seems not to be problematic in this case!