Skip to content

Commit 838cb7a

Browse files
authored
Revert "[Observation] Ensure deinitialized Observable types don't leave active observations in memory (#82752)" (#83492)
This reverts commit a349e64. This is in concert of #83436 <!-- If this pull request is targeting a release branch, please fill out the following form: https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1 Otherwise, replace this comment with a description of your changes and rationale. Provide links to external references/discussions if appropriate. If this pull request resolves any GitHub issues, link them like so: Resolves <link to issue>, resolves <link to another issue>. For more information about linking a pull request to an issue, see: https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue --> <!-- Before merging this pull request, you must run the Swift continuous integration tests. For information about triggering CI builds via @swift-ci, see: https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci Thank you for your contribution to Swift! -->
1 parent 0f05528 commit 838cb7a

File tree

2 files changed

+4
-55
lines changed

2 files changed

+4
-55
lines changed

stdlib/public/Observation/Sources/Observation/ObservationRegistrar.swift

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,9 @@ public struct ObservationRegistrar: Sendable {
110110
}
111111
}
112112

113-
internal mutating func deinitialize() -> (@Sendable () -> Void)? {
114-
func extractSelf<T>(_ ty: T.Type) -> AnyKeyPath {
115-
return \T.self
116-
}
117-
118-
var tracker: (@Sendable () -> Void)?
119-
lookupIteration: for (keyPath, ids) in lookups {
120-
for id in ids {
121-
if let found = observations[id]?.willSetTracker {
122-
// convert the keyPath into its \Self.self version
123-
let selfKp = _openExistential(type(of: keyPath).rootType, do: extractSelf)
124-
tracker = {
125-
found(selfKp)
126-
}
127-
break lookupIteration
128-
}
129-
}
130-
}
113+
internal mutating func cancelAll() {
131114
observations.removeAll()
132115
lookups.removeAll()
133-
return tracker
134116
}
135117

136118
internal mutating func willSet(keyPath: AnyKeyPath) -> [@Sendable (AnyKeyPath) -> Void] {
@@ -175,8 +157,8 @@ public struct ObservationRegistrar: Sendable {
175157
state.withCriticalRegion { $0.cancel(id) }
176158
}
177159

178-
internal func deinitialize() {
179-
state.withCriticalRegion { $0.deinitialize() }?()
160+
internal func cancelAll() {
161+
state.withCriticalRegion { $0.cancelAll() }
180162
}
181163

182164
internal func willSet<Subject: Observable, Member>(
@@ -207,7 +189,7 @@ public struct ObservationRegistrar: Sendable {
207189
}
208190

209191
deinit {
210-
context.deinitialize()
192+
context.cancelAll()
211193
}
212194
}
213195

test/stdlib/Observation/Observable.swift

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,6 @@ final class CowTest {
287287
var container = CowContainer()
288288
}
289289

290-
@Observable
291-
final class DeinitTriggeredObserver {
292-
var property: Int = 3
293-
var property2: Int = 4
294-
let deinitTrigger: () -> Void
295-
296-
init(_ deinitTrigger: @escaping () -> Void) {
297-
self.deinitTrigger = deinitTrigger
298-
}
299-
300-
deinit {
301-
deinitTrigger()
302-
}
303-
}
304-
305-
306290
@main
307291
struct Validator {
308292
@MainActor
@@ -527,23 +511,6 @@ struct Validator {
527511
expectEqual(subject.container.id, startId)
528512
}
529513

530-
suite.test("weak container observation") {
531-
let changed = CapturedState(state: false)
532-
let deinitialized = CapturedState(state: 0)
533-
var test = DeinitTriggeredObserver {
534-
deinitialized.state += 1
535-
}
536-
withObservationTracking { [weak test] in
537-
_blackHole(test?.property)
538-
_blackHole(test?.property2)
539-
} onChange: {
540-
changed.state = true
541-
}
542-
test = DeinitTriggeredObserver { }
543-
expectEqual(deinitialized.state, 1) // ensure only one invocation is done per deinitialization
544-
expectEqual(changed.state, true)
545-
}
546-
547514
runAllTests()
548515
}
549516
}

0 commit comments

Comments
 (0)