Skip to content

Memory leak issue with PassthroughRelay #167

Open
@just1103

Description

@just1103

Hello everyone,

I've been using CombineExt extensively in my projects and found it incredibly valuable.
Recently, I encountered a memory leak issue with PassthroughRelay. Upon replacing it with PassthroughSubject, the retain cycle disappeared.

It appears that PassthroughRelay fails to release its subscriptions upon receiving completion, as similarly described in this PR #85.

I've come up with a potential solution for this issue and confirmed its resolution in my project, but I'm not fully proficient with Combine. So, any comments or suggestions would be greatly appreciated.


Original Subscription’s cancel method:

func cancel() {
    sink = nil
}

Solution:

// Call forceFinish() in Subscription’s cancel method. 
// And add self.sink = nil in forceFinish method.

func forceFinish() {
    self.sink?.shouldForwardCompletion = true
    self.sink?.receive(completion: .finished)
    self.sink = nil // added
}

func cancel() {
    forceFinish() // replaced
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions