Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManagedAtomicLazyReference: Properly dispose object on deinit #69

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/Atomics/AtomicLazyReference.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
_storage = _Rep(nil)
}

deinit {
if let unmanaged = _storage.dispose() {
unmanaged.release()
}
}

@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<_Rep> {
_getUnsafePointerToStoredProperties(self).assumingMemoryBound(to: _Rep.self)
Expand Down
6 changes: 6 additions & 0 deletions Sources/Atomics/autogenerated/AtomicLazyReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public class ManagedAtomicLazyReference<Instance: AnyObject> {
_storage = _Rep(nil)
}

deinit {
if let unmanaged = _storage.dispose() {
unmanaged.release()
}
}

@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<_Rep> {
_getUnsafePointerToStoredProperties(self).assumingMemoryBound(to: _Rep.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2020-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -13,14 +13,14 @@
import XCTest
import Atomics

class UnsafeAtomicLazyReferenceTests: XCTestCase {
func test_create_destroy() {
class AtomicLazyReferenceTests: XCTestCase {
func test_unsafe_create_destroy() {
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
defer { v.destroy() }
XCTAssertNil(v.load())
}

func test_storeIfNilThenLoad() {
func test_unsafe_storeIfNilThenLoad() {
do {
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
XCTAssertNil(v.load())
Expand All @@ -38,10 +38,27 @@ class UnsafeAtomicLazyReferenceTests: XCTestCase {
XCTAssertEqual(LifetimeTracked.instances, 0)
}

func test_managed_storeIfNilThenLoad() {
do {
let v = ManagedAtomicLazyReference<LifetimeTracked>()
XCTAssertNil(v.load())

let ref = LifetimeTracked(42)
XCTAssertTrue(v.storeIfNilThenLoad(ref) === ref)
XCTAssertTrue(v.load() === ref)

let ref2 = LifetimeTracked(23)
XCTAssertTrue(v.storeIfNilThenLoad(ref2) === ref)
XCTAssertTrue(v.load() === ref)
}
XCTAssertEqual(LifetimeTracked.instances, 0)
}

#if !SWIFT_PACKAGE
public static var allTests = [
("test_create_destroy", test_create_destroy),
("test_storeIfNilThenLoad", test_storeIfNilThenLoad),
("test_unsafe_create_destroy", test_unsafe_create_destroy),
("test_unsafe_storeIfNilThenLoad", test_unsafe_storeIfNilThenLoad),
("test_managed_storeIfNilThenLoad", test_managed_storeIfNilThenLoad),
]
#endif
}
6 changes: 3 additions & 3 deletions Tests/AtomicsTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -44,8 +44,8 @@ var testCases = [
// DoubleWord
testCase(DoubleWordTests.allTests),

// UnsafeAtomicLazyReferenceTests
testCase(UnsafeAtomicLazyReferenceTests.allTests),
// AtomicLazyReference
testCase(AtomicLazyReferenceTests.allTests),
]

testCases += [
Expand Down