intuit/CardParts

Memory leak on hidden cards

nvonahsen opened this issue · 3 comments

Hey, I’m using cardparts in a current project and ran into an issue where old CardViewControllers were continuing to exist and causing crashes because I was clearing the data they were accessing.

Pretty sure the issue is line 140 in CardsViewController when using hidden traits

hiddenTrait.isHidden.asObservable().subscribe(onNext: { isHidden in
    DispatchQueue.main.async { [weak self] in
        self?.hideCard(cardInfo: cardInfo, isHidden: isHidden)
    }
}).disposed(by: bag)

Far as I can tell the [weak self] should be on the outer .subscribe, changing to this and recompiling seemed to fix my issue:

hiddenTrait.isHidden.asObservable().subscribe(onNext: { [weak self] isHidden in

As it stands it seems like a strong reference is passed into the closure and kept in memory and then used to create a seperate weak one for use within the async closure. So the subscription still maintains the strong reference to self so self cannot be removed from memory, and since self holds the dispose bag the subscription is never disposed so the controllers leak.

Seems a bit of a weird case since the strong reference self is never actually used within the subscription, but seems like swift still creates the strong reference for the outer closure and then uses that to create the weak reference for the inner one rather than simply passing through a weak one.

PS: if a fix comes out of this i would love it if you could let me know a rough release timeline because ill probably need to fork the repo till then

This is very interesting... let us take a look into this. We are planning a release sometime this week so we can include this fix if necessary. Thanks @nvonahsen !

Also, we would love to hear of any apps that use CardParts in production... so feel free to let us know and we can add your app to the README @nvonahsen ! 👍

Closing due to inactivity. Please reopen if you still have this issue.