apple / swift-atomics

Low-level atomic operations for Swift
Apache License 2.0
1.06k stars 50 forks source link

`ManagedAtomicLazyReference` leaks memory #66

Closed nkbelov closed 1 year ago

nkbelov commented 1 year ago

ManagedAtomicLazyReference leaks its contents.

Information

Checklist

Steps to Reproduce


final class Box<T: Sendable> {
    let value: T

    init(value: T) {
        self.value = value
    }

    deinit {
        print("BOX DEINIT")
    }
}

final class Item {

    private let test = ManagedAtomicLazyReference<Box<Int>>()

    init() {
        test.storeIfNilThenLoad(Box(value: 12345))
    }

    deinit {
        print("ITEM DEINIT")
    }
}

func test() {
    let _ = Item()
}
//`main.swift`:
test()

// prints "ITEM DEINIT" only

Expected behavior

"BOX DEINIT" and "ITEM DEINIT" get printed.

Actual behavior

Only "ITEM DEINIT" gets printed.

lorentey commented 1 year ago

Good catch! ManagedAtomicLazyReference fails to call dispose on its stored value.