Rxswift: [4.5] Memory leak

Created on 2 Apr 2019  路  2Comments  路  Source: ReactiveX/RxSwift

Short description of the issue:

Xcode 10.2, when configured to detect memory leaks (Malloc scribble, Malloc Stack -Live Only) is reporting a leak in AtomicInt/BinaryDisposable/DisposeBag. Just noticed this after upgrading to the latest xcode/rxswift combo. It's happening in a fairly generic viewcontroller that defines a dispose bag as a class let and sets up subscriptions in viewDidLoad(). There's nothing particularly special going on in this view controller so I'm fairly certain the issue is with Rx somehow.

Expected outcome:

No leaks

What actually happens:

Leaks

Self contained code example that reproduces the issue:

override func viewDidLoad() {
        super.viewDidLoad()

        someObservable
                    .map { $0.someObjectProperty }
                    .filter { !$0.isPropertySomeThingB && !$0.isPropertySomeThingB }
                    .distinctUntilChanged()
                    .observeOn(MainScheduler.instance)
                    .subscribe(onNext: { [weak self] value in self?.localFunction(value: value) })
                    .disposed(by: disposeBag)

}

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

4.5.0

Platform/Environment

  • [x] iOS
  • [ ] macOS
  • [ ] tvOS
  • [ ] watchOS
  • [ ] playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • [x] easy, 100% repro

Xcode version:

10.2

Installation method:

  • [x] CocoaPods
  • [ ] Carthage
  • [ ] Git submodules

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • [ ] yes (which ones)
  • [x] no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • [ ] just starting
  • [ ] I have a small code base
  • [x] I have a significant code base

Screen Shot 2019-04-01 at 5 55 12 PM

Most helpful comment

Hi @RamblinWreck77 ,

It is completely possible that we have leaks somewhere in AtomicInt but you haven't provided any proofs that we have them.

Automatic memory leaks detector is completely useless. The correct way to detect leaks is:

  • Create memory snapshot
  • Perform actions that should clean them, like poping VC.
  • Create another memory snapshot.
  • Check are the expected objects deallocated or not.

This is how you detect a memory leak by definition.

Automatic leak detector can't possibly know when should some object be deallocated. I'm not even sure why does Apple even ship it.

Right there on your screenshot there is a DisposeBag which holds a reference to BinaryDisposable and AtomicInts, which is normal.

All 2 comments

Hi @RamblinWreck77 ,

It is completely possible that we have leaks somewhere in AtomicInt but you haven't provided any proofs that we have them.

Automatic memory leaks detector is completely useless. The correct way to detect leaks is:

  • Create memory snapshot
  • Perform actions that should clean them, like poping VC.
  • Create another memory snapshot.
  • Check are the expected objects deallocated or not.

This is how you detect a memory leak by definition.

Automatic leak detector can't possibly know when should some object be deallocated. I'm not even sure why does Apple even ship it.

Right there on your screenshot there is a DisposeBag which holds a reference to BinaryDisposable and AtomicInts, which is normal.

@kzaher Interesting. Popping the parent VC definitely resulted in the "leak" disappearing. I've never seen a false-positive in my project/RxSwift before this update so I thought I'd share. Thanks!

Was this page helpful?
0 / 5 - 0 ratings