Skip to content

Commit 4954960

Browse files
authored
Fix memory leak in WithLatestFrom (#140)
1 parent 1f6b0df commit 4954960

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Sources/Operators/WithLatestFrom.swift

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ private extension Publishers.WithLatestFrom {
234234
sink = nil
235235
otherSubscription?.cancel()
236236
}
237+
238+
deinit { cancel() }
237239
}
238240
}
239241
#endif

Tests/WithLatestFromTests.swift

+23
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ class WithLatestFromTests: XCTestCase {
125125
XCTAssertNil(weakSubject2)
126126
}
127127

128+
func testWithResultSelectorDoesNotRetainClassBasedPublisherWithoutSendCompletion() {
129+
var upstream: AnyPublisher? = Just("1")
130+
.setFailureType(to: Never.self)
131+
.eraseToAnyPublisher()
132+
var other: PassthroughSubject<String, Never>? = PassthroughSubject<String, Never>()
133+
weak var weakOther: PassthroughSubject<String, Never>? = other
134+
135+
var results = [String]()
136+
137+
subscription = upstream?
138+
.withLatestFrom(other!) { "\($0)\($1)" }
139+
.sink(receiveCompletion: { _ in },
140+
receiveValue: { results.append($0) })
141+
142+
other?.send("foo")
143+
XCTAssertEqual(results, ["1foo"])
144+
145+
subscription = nil
146+
upstream = nil
147+
other = nil
148+
XCTAssertNil(weakOther)
149+
}
150+
128151
func testNoResultSelector() {
129152
let subject1 = PassthroughSubject<Int, Never>()
130153
let subject2 = PassthroughSubject<String, Never>()

0 commit comments

Comments
 (0)