Reputation: 33126
I am reading about RxSwift and there is a lot of discussion around memory leak. Here is quoting from the getting started guide on the main RxSwift repo:
If a sequence does not terminate on its own, such as with a series of button taps, resources will be allocated permanently unless
dispose
is called manually, automatically inside of a disposeBag, with the takeUntil operator, or in some other way.
And here is a snippet of code from Chapter 2: Observables of Reactive Programming with Swift where memory does leak:
Observable<String>.create({ observer in
observer.onNext("1")
// observer.onError(MyError.anError)
// observer.onCompleted()
return Disposables.create()
}).subscribe(
onNext: { print($0) },
onError: { print($0) },
onCompleted: { print("Completed") },
onDisposed: { print("Disposed") }
)
I understand that it is very important to dispose resources (with, for example, a dispose bag). However, what I don't understand is: what resources are leaking and how?
Upvotes: 4
Views: 1671
Reputation: 1403
It will be leaking because the chain and all of the resources that it captured will not be deallocated.
RxSwift allocates some objects that comprise the chain you subscribe to. They all take up memory and are necessary for the correct business logic of your Observable
chain. You might also pass some out of scope objects to the closures in your chain and they will also be retained since closures are a reference type and hold strongly to the resources they capture.
Upvotes: 1