Post

Replies

Boosts

Views

Activity

Reply to Memory leaks caused by closures
I think this is best illustrated with a capture list. You can capture the object, but it's not the default behavior. final class Object { var closure: () -> Object? = { nil } } @Test func test() { do { var object: Optional = Object() object!.closure = { object } weak var weakObject = object object = nil #expect(weakObject == nil) } do { var object: Optional = Object() object!.closure = { [object] in object } weak var weakObject = object object = nil #expect(weakObject != nil) } }
Oct ’24