Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in WithLatestFrom #140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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