Avatar
Avatar
yimajo
@swift-5.5.3 class Foo {} let foo = Foo() print("init Foo: refCount = \(_getRetainCount(foo))") do { var closure: (() -> ())? = { [bar = foo] in print("in closure: refCount = \(_getRetainCount(bar))") } print("before closure(): refCount = \(_getRetainCount(foo))") closure?() print("after closure?(): refCount = \(_getRetainCount(foo))") closure?() print("after closure?(): refCount = \(_getRetainCount(foo))") closure = nil } print("deinit capture: refCount = \(_getRetainCount(foo))") (edited)
init Foo: refCount = 2 before closure(): refCount = 3 in closure: refCount = 4 after closure?(): refCount = 3 in closure: refCount = 4 after closure?(): refCount = 3 deinit capture: refCount = 2