Skip to content

Commit 530c98a

Browse files
Address memory leak (#186)
Fixes #137. Co-authored-by: Francisco Ceruti <[email protected]>
1 parent bddb522 commit 530c98a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Sources/Sharing/Shared.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ public struct Shared<Value> {
393393
subjectCancellable = _reference.publisher.subscribe(subject)
394394
#endif
395395
}
396+
deinit {
397+
#if canImport(Combine)
398+
subjectCancellable.cancel()
399+
#endif
400+
#if canImport(SwiftUI)
401+
swiftUICancellable?.cancel()
402+
#endif
403+
}
396404
#if canImport(SwiftUI)
397405
func subscribe(state: State<Int>) {
398406
guard #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10) else { return }

Sources/Sharing/SharedReader.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ public struct SharedReader<Value> {
282282
subjectCancellable = _reference.publisher.subscribe(subject)
283283
#endif
284284
}
285+
deinit {
286+
#if canImport(Combine)
287+
subjectCancellable.cancel()
288+
#endif
289+
#if canImport(SwiftUI)
290+
swiftUICancellable?.cancel()
291+
#endif
292+
}
285293
#if canImport(SwiftUI)
286294
func subscribe(state: State<Int>) {
287295
guard #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10) else { return }

0 commit comments

Comments
 (0)