Skip to content

Commit

Permalink
Merge pull request #69 from lorentey/fix-leak
Browse files Browse the repository at this point in the history
ManagedAtomicLazyReference: Properly dispose object on deinit
  • Loading branch information
lorentey authored Mar 17, 2023
2 parents 71a1f86 + cb7a3b2 commit 6fcf644
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
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

0 comments on commit 6fcf644

Please sign in to comment.