Skip to content

Commit

Permalink
Fix memory leak in WithLatestFrom (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
TTOzzi committed Aug 5, 2022
1 parent 1f6b0df commit 4954960
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Operators/WithLatestFrom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ private extension Publishers.WithLatestFrom {
sink = nil
otherSubscription?.cancel()
}

deinit { cancel() }
}
}
#endif
23 changes: 23 additions & 0 deletions Tests/WithLatestFromTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ class WithLatestFromTests: XCTestCase {
XCTAssertNil(weakSubject2)
}

func testWithResultSelectorDoesNotRetainClassBasedPublisherWithoutSendCompletion() {
var upstream: AnyPublisher? = Just("1")
.setFailureType(to: Never.self)
.eraseToAnyPublisher()
var other: PassthroughSubject<String, Never>? = PassthroughSubject<String, Never>()
weak var weakOther: PassthroughSubject<String, Never>? = other

var results = [String]()

subscription = upstream?
.withLatestFrom(other!) { "\($0)\($1)" }
.sink(receiveCompletion: { _ in },
receiveValue: { results.append($0) })

other?.send("foo")
XCTAssertEqual(results, ["1foo"])

subscription = nil
upstream = nil
other = nil
XCTAssertNil(weakOther)
}

func testNoResultSelector() {
let subject1 = PassthroughSubject<Int, Never>()
let subject2 = PassthroughSubject<String, Never>()
Expand Down

0 comments on commit 4954960

Please sign in to comment.