RxSwiftCommunity / Action

Abstracts actions to be performed in RxSwift.
MIT License
875 stars 150 forks source link

Potential Memory Leak #206

Closed hakkurishian closed 5 years ago

hakkurishian commented 5 years ago

Possibly related to #41. Is it a misunderstanding (if so, how to handle it properly) or a memory leak that an action associated with a view controller gets not deallocated, and therefore its subscriptions disposed when the view controller gets destroyed?

Demo-Project

VC1 (List):

//before showing VC2 
detail.action.executionObservables.switchLatest()
.debug("segue subscription")
.subscribe(onNext:{_ in
print("subscriber")
            }).disposed(by: bag)

VC2 (Detail):

 lazy var action = Action<Void,Void>{_ in
        return .just(())
    }

Process

  1. VC1 is initially shown
  2. VC2 is initiallzed
  3. VC1 subscribes to VC2s actions output
  4. VC2 gets shown
  5. VC2 gets dismissed
  6. VC2 gets deinitialized

Expectation After 6. resources for the action ( property of VC2) get deallocated and the subscription to it disposed

Observation The Action and its Rx-related resources leak.

Problem I'm excessively using the Action framework in my app and deal with these issue all over it.

What would be the correct way to produce the expected behaviour and why, if its not a bug, is the behaviour above produced, leading to an accumulation of RX-related resources.

bobgodwinx commented 5 years ago

Hi @hakkurishian I took a look at the code and I think you making the Subscriptions in wrong way. If you want that resource to be deallocated when the second view is deallocated then you have to have a disposeBag in the second view and using it during prepare for segue. The problem you raised has nothing todo with Action it's self. It's about managing your view properly.

hakkurishian commented 5 years ago

Thank you, it worked perfectly. Indeed it was part of my misunderstanding on how DisposeBag worked.